### Basic Usage of React Keep Alive Components Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This example demonstrates the basic setup of `react-keep-alive`. The `` component must wrap all `` instances for caching to take effect. `` wraps the component (``) to be cached, requiring a unique `name` prop. ```JavaScript import React from 'react'; import ReactDOM from 'react-dom'; import { Provider, KeepAlive, } from 'react-keep-alive'; import Test from './views/Test'; ReactDOM.render( , document.getElementById('root'), ); ``` -------------------------------- ### Installing Latest React Router for Keep Alive Compatibility Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This command installs the `next` versions of `react-router` and `react-router-dom`. This is crucial because `react-keep-alive` utilizes the new React Context API, requiring compatible versions of other libraries like React Router. ```Bash npm install react-router@next react-router-dom@next ``` -------------------------------- ### Installing React Keep Alive with npm Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This command installs the `react-keep-alive` package and saves it as a dependency in your project's `package.json` file. It requires React 16.3 or later, with React Hooks requiring 16.8 or higher. ```Bash npm install --save react-keep-alive ``` -------------------------------- ### Rendering Root Component with React Keep Alive Provider Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This example shows how to wrap the root application component (``) with the `` component. The `` must be rendered at the top level of the application for the caching mechanism to function correctly. ```JavaScript import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-keep-alive'; import App from './App'; ReactDOM.render( , document.getElementById('root'), ); ``` -------------------------------- ### Applying `bindLifecycle` HOC to a React Component Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This example shows how to use the `bindLifecycle` Higher-Order Component (HOC) to augment a React class component. Components wrapped with `bindLifecycle` gain access to `componentDidActivate` and `componentWillUnactivate` lifecycle methods, enabling specific logic when a cached component becomes active or unactive. ```JavaScript import React from 'react'; import {bindLifecycle} from 'react-keep-alive'; @bindLifecycle class Test extends React.Component { render() { return (
This is Test.
); } } ``` -------------------------------- ### Using KeepAlive with React Router for Component Caching Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This example shows how to integrate `` with `react-router-dom`. A component (``) is wrapped by `` within a `Route`, ensuring its state is preserved when navigating away and back. The wrapped component must have a real DOM tag as its outermost layer. ```JavaScript import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Switch, Route, Link, } from 'react-router-dom'; import { Provider, KeepAlive, } from 'react-keep-alive'; class One extends React.Component { render() { return ( // a real DOM tag
This is One.
); } } class App extends React.Component { render() { return (
); } } ReactDOM.render( , document.getElementById('root'), ); ``` -------------------------------- ### Implementing Lifecycle Effects with React Keep Alive (JavaScript) Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This snippet demonstrates the `useKeepAliveEffect` hook from `react-keep-alive` to manage component lifecycle. It logs 'mounted' when the component is initialized and 'unmounted' when it is disposed, similar to `useEffect` but tailored for 'keep-alive' component contexts. It requires `react` and `react-keep-alive` as dependencies. ```JavaScript import React from 'react'; import {useKeepAliveEffect} from 'react-keep-alive'; function Test() { useKeepAliveEffect(() => { console.log("mounted"); return () => { console.log("unmounted"); }; }); return (
This is Test.
); } ``` -------------------------------- ### Configuring Provider `include` Prop for Caching Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md The `include` prop of `` specifies which components should be cached. It accepts a comma-separated string, an array of strings, or a regular expression to match component names. ```JavaScript ... // or ... // or ... ``` -------------------------------- ### Integrating React Keep Alive Provider with Router and Mobx Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This snippet demonstrates the correct nesting order when using `react-keep-alive` with `react-router-dom` and `mobx-react`. The `KeepAliveProvider` should be placed inside the `Router` component, and both should use the latest Context API for compatibility. ```JavaScript import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, } from 'react-router-dom'; import { Provider as MobxProvider, } from 'mobx-react'; import { Provider as KeepAliveProvider, } from 'react-keep-alive'; ReactDOM.render( , document.getElementById('root'), ); ``` -------------------------------- ### Combining KeepAlive with Provider's `include` Prop Source: https://github.com/structurebuilder/react-keep-alive/blob/master/README.md This snippet illustrates how to use the ``'s `include` prop to explicitly specify which `` components should be cached. Here, only the component named 'One' will be cached, even if other `` components exist without matching the `include` criteria. ```JavaScript import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Switch, Route, Link, } from 'react-router-dom'; import { Provider, KeepAlive, } from 'react-keep-alive'; class One extends React.Component { render() { return (
This is One.
); } } class App extends React.Component { render() { return (
); } } ReactDOM.render( , document.getElementById('root'), ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.