Challenges with Implementing Derived/Computed State in React Native Using Async Persistence with Legend State #332
Replies: 1 comment 2 replies
-
I think you don't want the computed to be async. That will end up waiting a tick every time food$ changes (await always waits even if the Promise is resolved). The food$ observable should sync itself so Also a little note, export const foodNamesByGroup$ = observable(() => {
const data = food$.get() || []
// Placeholder for computation logic
// compute new results object
return grouped
}) Does it work better that way? If not can you share more of your code so I can see what's going on? |
Beta Was this translation helpful? Give feedback.
-
I'm trying to implement a derived/computed state in a React component that involves asynchronous data persistence, and I'm struggling to find relevant examples or documentation. My application uses a Supabase backend to manage a 'food' table, from which I want to fetch rows and then group them by similarity into an array.
Here’s how I’ve set up my Legend State and Observable configurations:
Transitioning from a TanStack Query setup, where a query function fetched the data and computed the array directly, I attempted to create a similar computed observable with Legend State:
In the component:
I’ve experimented with await when(food$) and useEffect with useState, but these approaches lead to issues like undefined states, unfulfilled promises, or infinite re-renders.
Questions:
Is there a recommended pattern for managing asynchronous derived states in Legend State similar to what I could do with TanStack Query?
How can I ensure that the computed states update properly when the underlying data changes, considering the asynchronous nature of AsyncStorage?
Any insights or examples would be greatly appreciated, especially from those familiar with Legend State and asynchronous data handling in React.
Beta Was this translation helpful? Give feedback.
All reactions