originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.15k stars 225 forks source link

Plugin is not working with rtk query hooks #516

Open Sasha-meow opened 9 months ago

Sasha-meow commented 9 months ago

Versions

Reproduction

  1. clone repo from: https://github.com/Sasha-meow/federation
  2. To start remote app go to folder "remote", run npm i, then npm run build and npm run preview
  3. To start host app go to folder "federation", run npm i and npm run dev

Steps to reproduce

Button component in remote app includes useGetPokemonsQuery hook (made by redux toolkit). When you start host app - you can see the error: Uncaught TypeError: Cannot read properties of null (reading 'useMemo')

image It seems, that plugin doesn`t like something in useQuery hook inside rtk (you can find in call stack from screenshot)

I tried to install previos versions of your plugin - it didn't help me; tried to change shared dependencies config - it didn't help me too.

If remote app doesn't include rtk query hook, everything works fine (you can delete hook from remote>src>component>ButtonCustom and check it). If replace this hook to native function of the endpoint: everything works fine (you can delete query hook and uncomment dispatch and const { data } = await dispatch(baseApi.endpoints.getPokemons.initiate(undefined)) on 18 line in remote>src>component>ButtonCustom and check it)

omarviano commented 9 months ago

I have the same problem. Or rather, I think it's the same problem.

image

phogel commented 9 months ago

It seems like you're initialising the store too early, outside of the component, here: https://github.com/Sasha-meow/federation/blob/main/remote/src/component/MainContent/component.tsx#L12

If you move this line into the component body like:

const MainContent = ({ title }: { title: string }) => {
    const store = useMemo(() => setupStore(), []);
    ...
}

it works.

Sasha-meow commented 9 months ago

It seems like you're initialising the store too early, outside of the component, here: https://github.com/Sasha-meow/federation/blob/main/remote/src/component/MainContent/component.tsx#L12

If you move this line into the component body like:

const MainContent = ({ title }: { title: string }) => {
    const store = useMemo(() => setupStore(), []);
    ...
}

it works.

Actually, it didn't help me( Same error But you are right, store must be in component, I updated code

adirzoari commented 4 months ago

@Sasha-meow same issue, did you find a solution?

timwuhaotian commented 3 months ago

Having the same issue using https://lexical.dev/

image
timwuhaotian commented 3 months ago

It seems like you're initialising the store too early, outside of the component, here: https://github.com/Sasha-meow/federation/blob/main/remote/src/component/MainContent/component.tsx#L12 If you move this line into the component body like:

const MainContent = ({ title }: { title: string }) => {
    const store = useMemo(() => setupStore(), []);
    ...
}

it works.

Actually, it didn't help me( Same error But you are right, store must be in component, I updated code

Did you find a solution?