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

Persisted observable returns undefined during initial render #448

Closed
jabahere opened this issue Jan 25, 2025 · 4 comments
Closed

Persisted observable returns undefined during initial render #448

jabahere opened this issue Jan 25, 2025 · 4 comments

Comments

@jabahere
Copy link
Contributor

jabahere commented Jan 25, 2025

Hello,
Your library looks awesome! I'm trying it out to see if I could use it within my app, and I'm running into a tiny "issue".

When I use observable within my component:

const Component: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const test$$ = use$(test$);
  
  console.log({ test: test$ })
  
  return (...)
}

I get test$ is undefined during the initial render (app launch).
I'd like to avoid getting undefined on initial render if possible. is that possible to achieve?

This is how my test$ observable is defined:

export const supabase = createClient<Database>(supabaseUrl, supabaseAnonKey);

const customSynced = configureSynced(syncedSupabase, {
  persist: {
    plugin: observablePersistAsyncStorage({
      AsyncStorage,
    }),
  },
  generateId: randomUUID,
  supabase,
  changesSince: "last-sync",
  fieldCreatedAt: "created_at",
  fieldUpdatedAt: "updated_at",
  fieldDeleted: "deleted",
});

export const test$ = observable(
  customSynced({
    supabase,
    collection: "test",
    select: (from) => from.select("*"),
    actions: ["read", "create", "update", "delete"],
    realtime: true,
    persist: {
      name: "test",
      retrySync: true,
    },
    retry: {
      infinite: true,
    },
  }),
);

I'm assuming this is happening because I'm using AsyncStorage as my persistence layer?
I'm using "@legendapp/state": "^3.0.0-beta.24", within react-native project.

Thanks!

@jmeistrich
Copy link
Contributor

Yes, it's because AsyncStorage is async. If you want a synchronous persistence you could use the MMKV plugin or the new kv-store plugin which is in the latest version but not yet documented. Details are here: #444

@jabahere
Copy link
Contributor Author

jabahere commented Jan 25, 2025

Thanks @jmeistrich, new expo-sqlite kv store works great, thought there's a misleading error which I ended up fixing in my fork. Here's PR if you're open to contributions: https://github.com/LegendApp/legend-state/pull/449/files

@jmeistrich
Copy link
Contributor

Oh thanks, not sure how that mistake got through there!

@jmeistrich
Copy link
Contributor

Just released beta.26 with your PR, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants