-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React TypeError: Cannot read properties of null (reading 'useState') #294
Comments
遇到同样的问题了,请问有解决吗 |
the same. It looks no bad until importing react. the examples don't consist of any react imports |
@RudikS-git , @flyfishzy |
Can you provide a reproduction repo? @RudikS-git @toanil315 |
I created a reproduction for the original error with updated versions here: https://github.com/TimKolberger/issue-reproduction-vite-federated-modules I prepared the repo be easy to setup: git clone [email protected]:TimKolberger/issue-reproduction-vite-federated-modules.git repro-issue-294 \
&& cd $_ \
&& pnpm install \
&& pnpm build \
&& pnpm preview Error
_federation_shared_ is registered, but not used: Versions in host and remote project: "dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0",
...
},
"devDependencies": {
"@originjs/vite-plugin-federation": "1.1.14",
"@vitejs/plugin-react": "3.0.1",
"vite": "4.0.4",
...
} Host vite.config.ts: Remote vite.config.ts: |
I have the same issue. Any ideas on how to quickly fix it? |
I'm joining to the question . I just switch from create-react-app to vite and really love it. don't want to switch to webpack just to use webapack Module federation plugin |
If you just want to run it, so delete line shared: ['react', 'react-dom'], it work for me. Waiting for @flyfishzy fix it |
It still doesn't work I have a host application with React and two remotes with the same React version. I already tried a lot of combinations with shared options and no one works. @flyfishzy you are only my hope, I don't want to go back to CRA |
I have the same problem when using ant design. I also tried everything, nothing helps. When I add the ant design component, an error appears: Uncaught TypeError: Cannot read properties of null (reading 'useContext') |
@flyfishzy wrote:
@flyfishzy you can break the existing example "react-vite" in this repo just by adding "const [ state ] = React.useState(0);" to the Button.jsx. You don't even have to use the state variable anywhere. Here is the full modified Button.jsx file: import React from "react";
import "./Button.css";
export default ({caption = "Shared Button"}) => {
const [ state ] = React.useState(0);
return (
<button className='shared-btn'>{caption}</button>
)
} I am in the same boat as everyone else. I love Vite and need module federation and don't want to go back to webpack. Please advise. Thank you. |
Hello @flyfishzy I see that You picked up this issue few days ago, thats great! There are two minimal repos with reproduced issue. https://github.com/grzesiek-ds/vite-host |
Hello, I actually have the same problem with Vite 4. The problem does not occur with Vite 2.8. I see that work is in progress on it but I wanted to know what you advise. We have a rather urgent need to share a module present in a Vite config and to use it in a Webpack config. Do you recommend leaving Vite for now? |
@rastiben for me it's not working with Vite 2.8 neither (( Do you maybe have reproduction code? |
We have released a new version v1.2.0, please try this version and check if the problem is fixed. |
I confirm that it works perfectly! Thank you very much for the work! |
I updated to version 1.2. But I have the same error. As soon as a component from antd appears in the remote project, an error appears: Cannot read properties of null (reading 'useState'). |
Same error here,cann't use |
For me, it seems to work so far. Maybe try to make sure that the package really updated? I mean I needed to remove |
Year, it works finally, thks~ |
I'm facing the same issue in next.js 12 with the mocdule working with cra/craco |
I am also having this problem |
It started to work now to me. Before it was crashing in dev server but work fine in build mode. Now works for both. |
My problem was with component imports. If I had a component A of a microservice X that imported a component B of a microservice Y, and component B also imported a component of the microservice X, it gave an error. I solved it by creating a microservice that contained the common components and it no longer gives me an error. |
Hi, Just to add here and following the setup in react-vite example.
I'm using This or something similar has happened with other packages installed in remote. Unless remote is very simple, it tends to break in the build. I would really like to use this plugin and it would make a big difference in the architecture of my app, and I could be wrong, but it seems that it's not yet totally ready for React. |
same issue here, is this issue being looked at at all ? this make the plugin useless for React |
I am experiencing the same issue. Node 2.10.0 My team and I are trying to load in a bunch of web games from the remote react app to a host react app. Some of the games work (namely Phaser2 and Phaser3 games), but the unit game fails, and the only error we get is "TypeError: Cannot read properties of null (reading 'useState')" |
I believe this issue just like what everyone says, makes cc @ruleeeer or any other contributors? |
Your remote file only gets built when you |
I am using Remote (vite):
Host (webpack):
Even if I upgrade my host react version to This issue is really blocking me I don't have a confidence in integrating this plugin. Would appreciate any steps towards resolution. Thanks! |
@codeandgyan @rodoabad same issue, any solution? |
I have a similar problem, I am using xstate on my remote, when I call it from the host I get:
One solution that seems to work is to put xstate in shared on the host and remote. But I don't see the point in having the xstate dependency on my host since I don't need it there. Does anyone have any idea how to fix it? I don't want to switch to webpack I am using the following versions
|
I have exactly the same issue when using some 3rd party library in remote MFE. And Only if I put that dependency to the Host, then it works fine, but that shouldn't be like that, because Host don't need to know anything about that dependency... I Have a bad feeling that this plugin is useless for now... since this issue was raised more than a year ago and still it wasn't fixed. |
Any update????????? |
Any update?????? |
Any update? Still facing the same issue |
any update? |
The project seems to have been out of development for a long time |
In case anyone is interested... I ended up using webpack on my remote with |
Could you please share your webpack configuration in remote. |
Sure, I leave you the remote and host respectively.
|
Claro, te dejo el remoto y el host respectivamente
|
I've been exploring building a library with Vite + React, and I'm facing this issue. I am using the following versions
|
I wanted to give vite a try and just came across this issue. No idea why this happens or how to fix it. So sad. |
Any update on this issue? |
If you are building an NPM package and using it in a different application, you need to make sure that your package does not introduce two versions of React. Step 1: Add "peerDependencies": {
"react": "^18",
"react-dom": "^18"
} Step 2: Add export default defineConfig({
build {
rollupOptions: {
external: ["react", "react-dom"],
},
}
}); Done. If you need a more robust approach, use the below code instead: import packageJson from "./package.json";
export default defineConfig({
build {
rollupOptions: {
external: Object.keys(packageJson.peerDependencies),
},
}
}); |
Versions
Reproduction
Additional Details
clone this repo https://github.com/guoxiaxing/vite-FM,
cd into each directory and run pnpm install. cd remote directory and run pnpm build && npx vite preview --port=3001 --strictPort, then cd host directory and run pnpm run dev.
Steps to reproduce
above
What is Expected?
all working
What is actually happening?
but http://localhost:5173/ not work, and throw Uncaught TypeError: Cannot read properties of null (reading 'useState')
at react_production_min.useState
The text was updated successfully, but these errors were encountered: