diff --git a/src/usePersistedState.js b/src/usePersistedState.js index f1d7fbf..3aa593c 100644 --- a/src/usePersistedState.js +++ b/src/usePersistedState.js @@ -29,16 +29,19 @@ const usePersistedState = (initialState, key, { get, set }) => { const persistentSetState = useCallback( (newState) => { - const newStateValue = - typeof newState === 'function' ? newState(state) : newState; - - // persist to localStorage - set(key, newStateValue); - - setState(newStateValue); - - // inform all of the other instances in this tab - globalState.current.emit(newState); + // persist to localStorage, set state, then inform all of the other instances in this tab + if (typeof newState === 'function') { + setState((prevState) => { + const newStateValue = newState(prevState); + set(key, newStateValue); + globalState.current.emit(newStateValue); + return newStateValue; + }); + } else { + set(key, newState); + setState(newState); + globalState.current.emit(newState); + } }, [state, set, key] );