Hey 👋 We have a lot of React Query hooks for data fetching and we keep running into scenarios in which we need the data returned from these hooks within machines/actors.
I know we can sync data using useEffect and this works wonderfully for things that will also update during the lifecycle of a machine (e.g., because data was mutated outside of it). However, I'd like to avoid the boilerplate if at all possible.
The only solution we've found so far is using a parent/wrapper component that only renders the component which consumes the machine after data has finished loading.
function usePlaygroundMachine() {
// `data` will be undefined while data is fetching
const { data } = useSomeData();
return useMachine(() =>
createMachine<Context, Event>({
id: "playground",
context: {
// Will also be undefined, of course
someId: data.someId,
}
// ...
})
);
}
→ Is there any way to avoid useEffect and wrapper components?
Thank you!