react-dom.development.js:20662 Uncaught Error: Cannot read properties of null (reading 'useState') #1101
-
Hey, I am using our custom library which is bundled with Vite but does exclude React, and I checked and I only have one instance of React and React is defined. But on the first render and on the server render I am seeing the following error, when using VPS. I also tried to redefine the hook inside the project, and then I don't see the error anymore.
I know that this can happen if:
// in project
React.test = 'test'
...
// in component lib
console.log(React.test) // logs test The configs of my component lib for bundleing: /// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import dts from 'vite-plugin-dts';
// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
formats: ['es', 'cjs'],
name: '@workdigtital/component-library-react',
fileName: (format) => `index.${format}.js`
},
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM'
},
exports: 'named'
}
}
},
plugins: [react(), dts({ insertTypesEntry: true })],
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./src/tests/setup.ts'],
exclude: ['**/node_modules/**', '**/dist/**', '**/coverage/**', '**.stories**', '.storybook/**', '**.types**'],
coverage: {
all: true,
exclude: ['**/*.stories.tsx', '.storybook/**'],
provider: 'c8',
reporter: ['cobertura', 'html']
}
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@flaticon': path.resolve(__dirname, './node_modules/@flaticon')
}
}
}); I think I bundle something wrong so it can be properly consumed by VPS? I don't see this issue in projects where we don't use VPS. Can you maybe point me into a direction what else I could investigate? Appendix: The hook in the component library: function useWindowSize(): TWindowSize {
console.log('useWindowSize');
console.log('useWindowSize: React', React);
console.log('useWindowSize: useState', useState);
console.log('useWindowSize: useState', useEffect);
const [windowSize, setWindowSize] = useState<TWindowSize>({
width: 1600,
height: 1200
});
useEffect(() => {
function handleResize() {
console.log('handleResize', React);
setWindowSize({
width: window.innerWidth,
height: window.innerHeight
});
}
window.addEventListener('resize', handleResize);
handleResize();
return () => window.removeEventListener('resize', handleResize);
}, []);
return windowSize;
}
export default useWindowSize; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I digged into this a while ago and it seemed like React was doing some kind of dependency injection. My digging wasn't more conclusive than that. So I don't think I can be of much a help here. Let me know how you end up resolving this. |
Beta Was this translation helpful? Give feedback.
@brillout while creating the minimal repository I stumbled over the solution the issue was caused by a vite plugin https://socket.dev/npm/package/vite-plugin-circular-dependency
This was in our project and it seems to cause the error somehow.