-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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. |
Thanks for the answer. |
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.
The text was updated successfully, but these errors were encountered: