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

Using RecoilRoot via npm #2306

Open
sky8196 opened this issue Feb 20, 2024 · 2 comments
Open

Using RecoilRoot via npm #2306

sky8196 opened this issue Feb 20, 2024 · 2 comments

Comments

@sky8196
Copy link

sky8196 commented Feb 20, 2024

env:
"react": "^18.2.0"
"recoil": "^0.7.7"

The recoilWrapped function is encapsulated and exported in an npm package as follows:
export default function recoilWrapped<T extends JSX.IntrinsicAttributes>(WrappedComponent: (props: T) => JSX.Element) { return (props: T) => { return ( <RecoilRoot> <WrappedComponent {...props} /> </RecoilRoot> ); }; }

This function is used in other projects by importing it from the npm package:
import recoilWrapped from '@my-npm';
recoilWrapped(App);

Under normal circumstances, switching routes would result in a state reset. However, when using recoilWrapped, all other features of Recoil operate as expected, except that the state does not get reset.

@sky8196 sky8196 changed the title 跨npm 使用RecoilRoot 通过 npm 使用RecoilRoot Feb 20, 2024
@sky8196 sky8196 changed the title 通过 npm 使用RecoilRoot Using RecoilRoot via npm Feb 20, 2024
@letscodedanish
Copy link

Here are a few things you can check and try to troubleshoot the issue:

Check Router Configuration: Ensure that your router configuration is correctly set up to unmount and remount components when switching routes. Different routers handle state preservation differently, so make sure that your router is configured to unmount components when navigating away from them.

Verify Recoil Root Placement: Ensure that the RecoilRoot component provided by Recoil is wrapping your entire application at the highest level. If RecoilRoot is not wrapping your entire application, state persistence might not work as expected.

Test State Reset Directly: Try manually resetting the state in your components when switching routes to see if the issue persists. You can do this by calling Recoil's resetAll or resetAllAtoms functions when navigating away from a route.

Check for Component Memory Leaks: Make sure that your components are properly cleaning up any subscriptions, event listeners, or other resources when they are unmounted. Memory leaks can sometimes prevent components from being fully unmounted, leading to unexpected behavior with state persistence.

Review Dependency Tree: Double-check the dependencies of your components and the recoilWrapped function to ensure that there are no conflicts or unexpected interactions that could be causing the state to persist between route changes.

Update Recoil and React Versions: Ensure that you are using the latest versions of Recoil and React, as older versions may have bugs or limitations related to state management and component lifecycle.

Check for Side Effects: Review your code for any side effects or asynchronous operations that could be interfering with state management. Make sure that all state updates are synchronous and predictable.

By following these steps and carefully reviewing your code and configuration, you should be able to identify and resolve the issue with state not resetting when switching routes in your React application using Recoil.

@sky8196
Copy link
Author

sky8196 commented Mar 1, 2024

Thanks for the answer.
I tested the points listed in the examples and found no problems.
Because of demand issues, I actually don't need the persistence of the state. I need it to be reset when the page switches.
I conducted a test. When I moved recoilWrapped to the current project and used it again, the data was reset and other functions were normal, which was in line with my expectations.
The usage method has not changed, but the source has been changed; from import recoilWrapped from '@my-npm'; to import recoilWrapped from '@/utils/recoilWrapped';.

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

2 participants