Skip to content
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

State and reducers are not removed when dynamic component unmounts #167

Open
madrussa opened this issue Jan 11, 2021 · 2 comments
Open

State and reducers are not removed when dynamic component unmounts #167

madrussa opened this issue Jan 11, 2021 · 2 comments

Comments

@madrussa
Copy link

When mounting a module using the DynamicModuleLoader and then later unmounting it, the state and reducers remain.

This can be verified by removing a DynamicModuleLoader and inspecting Redux using redux dev tools. The state applied when the module was loaded remains after the component is removed.

I have a solution but this took a while to work out what was going on, storing here for those who come across this same issue. The fix would be to document this feature.

Solution:
There is an undocumented strictMode property you can add to the loader. If you are using strict mode you must add this to all of your dynamic modules.

<DynamicModuleLoader key="myModule" modules={[getMyModule()]} strictMode>
    <MyComponent />
</DynamicModuleLoader>

/**
* Set this flag to indicate that this component is being rendered in 'Strict Mode'
* React 'StrictMode' does not allow constructor side-effects, so we defer adding modules to componentDidMount
* when this flag is set.
* This has the effect of adding a second render.
*/
strictMode?: boolean;

As shown above, what happens if you do not include the flag is the module manager will add the module twice, but only removes it once. This has the side effect of causing the reference counter to always think the module is still required.

@madrussa madrussa changed the title State and reducers are not unmounted when dynamic component unmounts State and reducers are not removed when dynamic component unmounts Jan 11, 2021
@luox12
Copy link

luox12 commented Jul 30, 2021

thanks @madrussa for your resolution.

@Pabliomen
Copy link

Thanks @madrussa this really helped me! works like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants