#createStore from fetched data ?

6 messages · Page 1 of 1 (latest)

fossil sonnet
#

Hello!
I'm sorry for asking this because I'm sure this is stupid. But here I go:
I'm fetching kind of heavy data from my own API and using useRouteData() in my component, and would like to display it in a table with actions, like delete, update some values, etc.
So far so good, I'm getting the data, but it takes a few seconds, and my createStore call isn't waiting for the data to be available :
const routeData = useRouteData();
const [seriesRows, setSeriesRows] = createStore(routeData()?.map((data: Series) => ({
...data,
deleteSeries,
someMoreStuff
})) as SeriesRow[]);

What is the best, cleanest way to wait for routeData and then create the store?
Thank you for your time!
Wishing you a nice day.

coarse whale
fossil sonnet
fossil sonnet
#

It's working like a charm with createEffect actually, but I'd like some better practice. Any advice ?

flint sinew
#

You should be careful of infinite loops when updating state in createEffect.

fossil sonnet
#

You're right. Right now I don't think I'll be having that problem, since the effect is depending on routeData, which isn't supposed to change, I guess ?
But I'll pay attention. I was just wondering if it was bad practice, since I'm pretty sure I read in the documentation that createEffect wasn't supposed to be used to update state, and that I should use createMemo instead. But maybe I understood that wrong 😳