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
Hello and thanks for this powerful react-frontload library!
I'm wondering how I can implement reload client-side.
My use case is the following. I have one high level hook that retrieves data (SSR and client-side) with useFrontload. I would like the hook to provide a reload function for the UI to be able to refresh data when the user click on something.
functionmyHook(){const{ data }=useFrontload('my-component',async({ api })=>{conststuff=awaitapi.getStuff();returnstuff})return{
data,
reload,// how to implement this reload function that will fetch data again client-side?}}
As far as I understand this is not currently possible because the useEffet inside useFrontload only run once, on mount:
Would you accept a PR that exposes a reload function from useFrontload result? This function would internally increment a useState counter used as a dependency for the useEffect, forcing it to run again.
const[forcedReload,setForcedReload]=React.useState<number>(0)React.useEffect(()=>{// current use effect},[forcedReload])return{
...state,setData: (fn: (data: T)=>T)=>{setState((state)=>({
...state,data: fn(state.data),}))},reload: ()=>{// adding thissetForcedReload(state=>state+1)}}
Let me know if it looks good for you :) Thanks!
The text was updated successfully, but these errors were encountered:
Hello and thanks for this powerful react-frontload library!
I'm wondering how I can implement reload client-side.
My use case is the following. I have one high level hook that retrieves data (SSR and client-side) with
useFrontload
. I would like the hook to provide areload
function for the UI to be able to refresh data when the user click on something.As far as I understand this is not currently possible because the
useEffet
insideuseFrontload
only run once, on mount:react-frontload/src/index.tsx
Line 474 in a206364
Am I missing something?
Would you accept a PR that exposes a reload function from
useFrontload
result? This function would internally increment auseState
counter used as a dependency for theuseEffect
, forcing it to run again.Let me know if it looks good for you :) Thanks!
The text was updated successfully, but these errors were encountered: