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
But getContext() doesn't work (yet) with these approaches (see also vikejs/vike-react#98).
Design
useData
// TodoItem.tsximport{useData}from'telefunc/react'import{onLoadTodoItem}from'./TodoItem.telefunc.ts'functionTodoItem(){constdata=useData(()=>onLoadTodoItem(id),// Dependency array like useEffect()[id])return<><span>{data.text}</span></>}
Alternative design:
// TodoItem.tsximport{useData}from'telefunc/react'import{onLoadTodoItem}from'./TodoItem.telefunc.ts'functionTodoItem(){// No need to pass a dependency array!constdata=useData(onLoadTodoItem)(id)return<><span>{data.text}</span></>}
// MovieList.tsximport{useDataState}from'telefunc/react'import{onMovieFetch}from'./MovieList.telefunc.ts'functionMovieList(){return(<div><p>List of movies:</p><Suspensefallback={<p>Loading...</p>}><List/></Suspense></div>)}functionList(){// Similar to useStateconst[movies,setMovies]=useDataState(// Async Initial dataasync()=>awaitonMovieFetch(),// Dependency array like useEffect()[])return(<ul>{movies.forEach((movie)=>(<li>{movie.title} ({movie.release_date})</li>))}<buttononClick={async()=>setMovies(awaitonMovieFetch())}>
Refetch
</button></ul>)}
PoC
There is also an integration with react-streaming built directly into Telefunc, but it isn't ready for public usage yet and breaking change will occur.
Warning
You can play around with it at /examples/react-streaming. But you're warned: the API as it is today will be deprecated.
Currently, Telefunc cannot be used for fetching the initial data of a page. (You have to use something like Vike's
+datahook instead.)Workarounds
If you use
vike-react-query, then see vike-react-query > Usage with Telefunc.You can use Telefunc with the
useAsync()component hook ofreact-streaming. (If you usevike-react,vike-reactalready installsreact-streaming.)But
getContext()doesn't work (yet) with these approaches (see also vikejs/vike-react#98).Design
useDataAlternative design:
See also:
useId()not working inside<Suspense>facebook/react#24669 (comment)useId()across Suspense for element-scoped caching reactjs/rfcs#275useDataStatePoC
There is also an integration with
react-streamingbuilt directly into Telefunc, but it isn't ready for public usage yet and breaking change will occur.Warning
You can play around with it at
/examples/react-streaming. But you're warned: the API as it is today will be deprecated.