react-backstate-preserve
is a React library designed to persist component states across browser navigation, such as the back and forward button usage. It prevents state reset when navigating back to a component, ensuring a smoother user experience.
- State Persistence: Maintains the state of React components during browser navigation.
- Easy to Use: Implements a custom React hook and context provider to seamlessly integrate with your existing React applications.
- TypeScript Support: Fully supports TypeScript for type safety and better development experience.
Install react-backstate-preserve
with npm:
npm install react-backstate-preserve
Or with yarn:
yarn add react-backstate-preserve
Import and wrap your components with StateHistoryProvider
and use useStateWithHistory
hook to preserve the state.
import { StateHistoryProvider, useStateWithHistory } from 'react-backstate-preserve';
function YourComponent() {
const [value, setValue] = useStateWithHistory<string>('', 'unique-key');
return (
<input value={value} onChange={(e) => setValue(e.target.value)} />
);
}
function App() {
return (
<StateHistoryProvider>
<YourComponent />
</StateHistoryProvider>
);
}
Contributions are always welcome! Please read the contribution guidelines first.
Distributed under the MIT License.