originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.39k stars 241 forks source link

TypeError: Cannot read properties of null (reading 'useContext') #618

Closed dawidmarczakcompany closed 4 months ago

dawidmarczakcompany commented 4 months ago

Versions

Reproduction

Vite config of the host app

export default defineConfig({
  plugins: [
    react(),
    federation({
      name: "my-apps",
      remotes: {
        dummy: "", //Need to provide some dummy declaration; https://github.com/originjs/vite-plugin-federation/discussions/159#discussioncomment-7633645
      },
      shared: ["react", "react-dom"],
    }),
  ],
  build: {
    target: "esnext",
  },
});

Vite config of the remote app

export default defineConfig({
  plugins: [
    react(),
    federation({
      name: 'mf,
      filename: 'mf.js',
      exposes: {
        './App': './src/components/App/App.tsx',
      },
      shared: ['react', 'react-dom'],
    }),
  ],
  build: {
    target: 'esnext',
  },
});

What is Expected?

Remote loads correctly

What is actually happening?

I got error - TypeError: Cannot read properties of null (reading 'useContext')

Anyone have solution for this?

LechLee commented 4 months ago

Might wan to try

export default defineConfig({
  plugins: [
    react(),
    federation({
      name: "my-apps",
      remotes: {
        dummy: "", //Need to provide some dummy declaration; https://github.com/originjs/vite-plugin-federation/discussions/159#discussioncomment-7633645
      },
      shared: {

   react: { singleton: true, requiredVersion: '^18.2.0' },
    'react-dom': { singleton: true, requiredVersion: '^18.2.0' },
      },
    }),
  ],
  build: {
    target: "esnext",
  },
});
dawidmarczakcompany commented 4 months ago

I found the solution - in the "shared" object I had to add also "react-redux" as I had this library used in host and microfront.

Errors from module federation are unclear in this case, and they didn't say anything about Redux.