Skip to content
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

Persist doesn't work as expected #64

Open
NorouziM opened this issue Sep 16, 2023 · 8 comments
Open

Persist doesn't work as expected #64

NorouziM opened this issue Sep 16, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@NorouziM
Copy link

NorouziM commented Sep 16, 2023

Description

Hello, I've enabled the "persist" option, which stores data in local storage. However, when I refresh the page, the data is replaced with the initial state, effectively removing the stored data. The documentation lacks a clear example of how this feature should work. Simply setting the data to local storage without retrieving it during hydration (page reload) doesn't achieve the intended purpose of persisting.

My code:

const invoiceSlice = createStore("invoice")(
  {
    cardPaymentImagesUploadedPaths: [] as Array<string>,
    form: {
      data: {
        user_name: "",
        user_phone: ""
      },
      isValid: true
    }
  },
  {
    persist: {
      enabled: true
    }
  }
).extendActions((set, get) => ({
  removeCardPaymentImagePath: (index: number) => {
    const urls = get.cardPaymentImagesUploadedPaths();
    set.cardPaymentImagesUploadedPaths(urls.filter((_, i) => i !== index));
  }
}));

@NorouziM NorouziM added the bug Something isn't working label Sep 16, 2023
@danobot
Copy link

danobot commented Sep 16, 2023

I'm having issues getting it to work as well.
see #63

@danobot
Copy link

danobot commented Sep 18, 2023

@NorouziM Can you try adding a logging middleware to understand if the store is rehydrated? I added an example below. This helped me eliminate zustood/zustand as a root cause.


export const zustandLogMiddlewareFactory = name => (config) => (set, get, api) =>
  config(
    (...args) => {
      console.log(name + ' Zustand current state', get());
      
      set(...args)
      console.log(name + ' Zustand new state',  get())
    },
    get,
    API
  );

Add it to Zustood options:

const myZustoodOptions = {
  middlewares: [zustandLogMiddlewareFactory('myStore')],
  persist: myPersistOptions,
}

@NorouziM
Copy link
Author

NorouziM commented Oct 8, 2023

Screenshot 2023-10-08 at 10 00 55
This is the result after reloading the page. Initially, we have the previous state stored in localStorage, but it is subsequently cleared.
@danobot

@RAFA3L
Copy link

RAFA3L commented Dec 4, 2023

On my first attempt the store was reinitialized too, but I notice it was a useEffect with the initial data. Removing the useEffect now it works fine.

@alan-chen-la-478
Copy link

alan-chen-la-478 commented Jan 19, 2024

I'm having the same issue and couldn't figure out a way to make it work.

store.js

    const name = 'app';
    const store = createStore(name, {
        persist: {
            enabled: true,
            name: `__presist_store__${name}`,
            storage: createJSONStorage(() => AsyncStorage),
        },
    })({
        access_token: null
    );

app.js

const App = () => {
    const accessToken = store.use.access_token();

    const loadToken = async() => {
        // some api stuff
        store.set.access_token(response.token);
    }

    return (
        <>
            <Text>{accessToken}</Text>
            <Pressable onPress={loadToken}><Text>Load token</Text></Pressable>
        </>
    );
}

My access token is always null when app is relaunch or reload via dev tool. How an I make the data persist? It used to work when using zustood "@udecode/zustood": "^1.1.3". Yes, I'm using react-navigation in my app. Can any show me a working setup? thanks.

@elijaholmos
Copy link

My app crashes when trying to use the persist middleware. Here's the error I get and the implementation I have:
image
image

This issue has been open for a while without any attention from the maintainer. @zbeyens, can we please get some support with this?

@joeyfigaro
Copy link

@zbeyens are you accepting contributions for this? no working rehydration means we can't use this lib.

@zbeyens
Copy link
Member

zbeyens commented May 25, 2024

Sure, I'd merge it asap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants