Sharing a new library for RTK state persistence: rtk-persist
👋
#5059
tommasoberlose
started this conversation in
Show and tell
Replies: 2 comments 7 replies
-
Isn’t this already supported by the library?
…On Tue, 26 Aug 2025 at 1:48 PM, Tommaso Berlose ***@***.***> wrote:
Hey everyone,
I've been working on a little library to help with state persistence in
Redux Toolkit and wanted to share it with the community to get your
thoughts. It's called rtk-persist.
You can find the repository here:
https://github.com/fancypixel/rtk-persist 🚀
Why I built this 🤔
My main goal was to create a simple way to save and rehydrate just *specific
slices* of the Redux store, instead of having to deal with the whole
thing. I wanted to build a modern tool that feels like a natural extension
of the Redux Toolkit ecosystem.
I tried to focus on a few key ideas:
- *Simplicity*: A straightforward API that's easy to get started with.
- *RTK-Native Feel*: It's designed to fit right in with configureStore
and your existing slices.
- *TypeScript-Friendly*: It's written in TypeScript with type safety
in mind.
Major Features 🌟
- *Effortless Persistence*: Persist any Redux Toolkit slice or reducer
with minimal configuration.
- *Asynchronous Rehydration*: Store creation is now asynchronous,
ensuring that your app only renders after the state has been fully
rehydrated.
- *Seamless Integration*: Designed as a drop-in replacement for RTK
functions. Adding or removing persistence is as simple as changing an
import.
- *React Redux Integration*: Comes with a <PersistedProvider /> and a
usePersistedStore hook for easy integration with React applications.
- *Flexible API*: Choose between a createPersistedSlice utility or a
createPersistedReducer builder syntax.
- *Nested State Support*: Easily persist slices or reducers that are
deeply nested within your root state using a simple nestedPath option.
- *Storage Agnostic*: Works with any storage provider that implements
a simple getItem, setItem, and removeItem interface.
- *TypeScript Support*: Fully typed to ensure a great developer
experience with path validation.
- *Minimal Footprint*: Extremely lightweight with a production size
under 15 KB.
How it works 🛠️
The library works by wrapping the specific slice you want to persist.
1. Create the Slice
Replace createSlice with createPersistedSlice. The function accepts the
same options.
// features/counter/counterSlice.tsimport { createPersistedSlice } from 'rtk-persist';import { PayloadAction } from ***@***.***/toolkit';
export const counterSlice = createPersistedSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
},});
export const { increment, decrement } = counterSlice.actions;export default counterSlice.reducer;
2. Configure the Store
Then you must use configurePersistedStore and provide a storage handler.
The store creation is *asynchronous* and returns a promise that resolves
with the rehydrated store.
// app/store.tsimport { configurePersistedStore } from 'rtk-persist';import { counterSlice } from '../features/counter/counterSlice';// import { counterReducer } from '../features/counter/counterReducer';
// For web, use localStorage or sessionStorageconst storage = localStorage;
export const store = configurePersistedStore(
{
reducer: {
// IMPORTANT: The key must match the slice's `name` or the reducer's `name`.
[counterSlice.name]: counterSlice.reducer,
// [counterReducer.reducerName]: counterReducer,
},
},
'my-app-id', // A unique ID for your application
storage);
// Note: RootState and AppDispatch types need to be inferred differently// due to the asynchronous nature of the store.// This is typically handled within your React application setup.export type Store = Awaited<typeof store>;export type RootState = ReturnType<Store['getState']>;export type AppDispatch = Store['dispatch'];
Usage with React ⚛️
For React apps, there's a <PersistedProvider /> component that handles
waiting for the state to be rehydrated before rendering your UI. This helps
prevent any UI flicker on the initial load.
// main.tsximport React from 'react';import ReactDOM from 'react-dom/client';import App from './App';import { PersistedProvider } from 'rtk-persist/integrations/react-redux';import { store } from './state/store'; // This is the promise from configurePersistedStore
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<PersistedProvider store={store} loader={<div>Loading...</div>}>
<App />
</PersistedProvider>
</React.StrictMode>,);
I'd love to hear what you think! 🙏
I'm still actively developing it and would be really grateful for any
feedback, suggestions, or ideas you might have. Please let me know what you
think!
Thanks so much for checking it out. 😊
—
Reply to this email directly, view it on GitHub
<#5059>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAUG6PERII25MXNV3ITKDK33PQJ5NAVCNFSM6AAAAACE2FY2M6VHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZYHAYDCOBQGU>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
3 replies
-
If I want to persist a part of an already created slice? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone,
I've been working on a little library to help with state persistence in Redux Toolkit and wanted to share it with the community to get your thoughts. It's called
rtk-persist
.You can find the repository here: https://github.com/fancypixel/rtk-persist 🚀
Why I built this 🤔
My main goal was to create a simple way to save and rehydrate just specific slices of the Redux store, instead of having to deal with the whole thing. I wanted to build a modern tool that feels like a natural extension of the Redux Toolkit ecosystem.
I tried to focus on a few key ideas:
configureStore
and your existing slices.Major Features 🌟
<PersistedProvider />
and ausePersistedStore
hook for easy integration with React applications.createPersistedSlice
utility or acreatePersistedReducer
builder syntax.nestedPath
option.getItem
,setItem
, andremoveItem
interface.How it works 🛠️
The library works by wrapping the specific slice you want to persist.
1. Create the Slice
Replace
createSlice
withcreatePersistedSlice
. The function accepts the same options.2. Configure the Store
Then you must use
configurePersistedStore
and provide a storage handler. The store creation is asynchronous and returns a promise that resolves with the rehydrated store.Usage with React ⚛️
For React apps, there's a
<PersistedProvider />
component that handles waiting for the state to be rehydrated before rendering your UI. This helps prevent any UI flicker on the initial load.I'd love to hear what you think! 🙏
I'm still actively developing it and would be really grateful for any feedback, suggestions, or ideas you might have. Please let me know what you think!
Thanks so much for checking it out. 😊
Beta Was this translation helpful? Give feedback.
All reactions