You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today in a react-router framework app I can return data from server loaders and client loaders (as well as actions) but from what I can tell I cannot "push" data as it changes. Instead of resorting to hooks in React components for event sources, web sockets, etc. I'd much rather work with an async iterable (e.g. an async generator function) or a readable stream and have react-router automatically grab the latest yielded value as my loaderData. This way I can keep track of single or multiple sources of data independent of React components (clear separation of concerns like there is with non-real-time data today) and React components can automatically re-render as the latest props are passed in.
The below example returns the time with 1 second intervals to demonstrate:
exportasyncfunction*clientLoader(){while(true){delay(1_000);yield{time: Date.now()};}}exportfunctionDemo({loaderData: { time }}){return<div>{newDate(time).toLocaleString()}</div>;}
This pattern can be used with an EventSource (server-sent-events), a web socket, watching for database changes, etc.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Today in a react-router framework app I can return data from server loaders and client loaders (as well as actions) but from what I can tell I cannot "push" data as it changes. Instead of resorting to hooks in React components for event sources, web sockets, etc. I'd much rather work with an async iterable (e.g. an async generator function) or a readable stream and have react-router automatically grab the latest yielded value as my
loaderData
. This way I can keep track of single or multiple sources of data independent of React components (clear separation of concerns like there is with non-real-time data today) and React components can automatically re-render as the latest props are passed in.The below example returns the time with 1 second intervals to demonstrate:
This pattern can be used with an
EventSource
(server-sent-events), a web socket, watching for database changes, etc.Beta Was this translation helpful? Give feedback.
All reactions