-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add ability to delete, or choose not to rehydrate the store in onRehydrateStorage #405
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
Comments
Looking forward to it! |
@mirshko @barbogast @AnatoleLucet How I managed to make
|
Another way to do this would be to make getStorage: () => {
const state: IAppState = JSON.parse(localStorage.getItem("STORAGENAME"))?.state;
const timedout = Date.now() - state._timestamp > TIMEOUT;
if (timedout) localStorage.removeItem("STORAGENAME");
return localStorage;
}, |
It's been over a year. Any progress on this? Is I want to disable the auto rehydrate completely, and manually rehydrate after. So |
@dai-shi @AnatoleLucet having this kind option would probably fix this issue. I'm not very in tune with the library's ins and outs and not sure if this thing is possible. |
Wonder how @AnatoleLucet would like to do with these open issues. |
Can anyone help understanding what should be implemented for this issue? |
I'm not sure if this is 100% the same issue, but I had issues working on a SSR app where I need to control when the persist middleware triggers to respect the frameworks own rendering / hydration life cycle. It feels similar to the OP problem just different application. I've made a fork with a very simple change that will skip the initial hydration, allowing you to decide when the hydration is triggered. It's pretty self explanatory but a rough example with react context provider: const createBasketStore = ({ initialItems }: BasketStoreProps) => {
const store = createStore<BasketState>()(
persist(
(set, get) => ({
items: initialItems || [],
address: null,
...
}),
{ name: "basket", manualHydration: true }
)
);
return store;
};
export function BasketProvider({ children }: { children: ReactNode }) {
const storeRef = useRef<BasketStore>();
if (!storeRef.current) {
storeRef.current = createBasketStore({});
}
// Hydrate from local storage onMount
useEffect(() => {
if (storeRef.current) {
storeRef.current.persist.rehydrate();
}
}, []);
return (
<BasketContext.Provider value={storeRef.current}>
{children}
</BasketContext.Provider>
);
} I'd be happy to contribute more on this if you think it's a valid feature. |
@gmanninglive
Look forward to it! |
Great I'll get a PR sorted with tests and docs 😄 |
Is this getting resolved or nobody looking at it ? |
Sorry i started a PR then work commitments got in the way. I'll have one out for review this eve, just need to add the docs |
* add manual hydration option to persist middleware * rename variable, update jsdoc, remove from oldImpl * add tests for persist skipHydration * add docs * Update docs/integrations/persisting-store-data.md Co-authored-by: Blazej Sewera <code@sewera.dev> * Update docs/integrations/persisting-store-data.md Co-authored-by: Blazej Sewera <code@sewera.dev> * Update src/middleware/persist.ts --------- Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> Co-authored-by: Blazej Sewera <code@sewera.dev>
This might have overlap with the issue #267, however this isn't fully explained in that.
I notice there isn't a great way to handle conditional persisted state rehydration.
In the
onRehydrateStorage
it would be nice to be able to either returnstate
or throw an Error / return directly to not rehydrate the storage.A little example; here I want to check if a cookie is still present, a JWT that expires after a week, and then choose not to rehydrate, the only option I found is below, to delete the localStorage key.
An ideal API would be,
Let me know if I'm missing something!
The text was updated successfully, but these errors were encountered: