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
For example, this could be caused by immediate error callback of API client.
functionuseStore(initialState=0){const[state,setState]=useState(initialState)constonInput=(input)=>setState({ ...state, input })constonRequestStart=()=>setState({ ...state,loading: true,sentInput: state.input})constonRequestFailed=()=>setState({ ...state,loading: false})return{ state, decrement, increment }}const[state,actions]=Store.useContainer()actions.onRequestStart()api.call(params,(data,error)=>{if(error)actions.onRequestFailed()// could reference old state value, "sentInput" value will be lost.})
This is standard react hooks behavior — your count variable is closed-over when creating your decrement behavior. In this case you probably want to use the reducer form of setState or just use useReducer directly.
You'll also want to wrap those callbacks in useCallback to avoid unnecessary re-renders.
The answer by @shrugs should be added to the official documentation. I think there are also cases where decrement and increment should just have their own context, without count. That's similar to how useReducer is recommended with context.
then
For example, this could be caused by immediate error callback of API client.
Maybe related: #26
The text was updated successfully, but these errors were encountered: