How react-keep-alive works
masterUnlike solutions that use display: none | block, react-keep-alive uses the React.createPortal API.
- The
<Provider>component is responsible for saving the component's cache and rendering the cached component outside of the application tree via a portal. - The
<KeepAlive>component mounts these cached components from the portal back into the location where they need to be displayed.
This approach allows for smooth animations and full support for React Hooks, as components are not simply hidden but managed through portals.