( resetKey: unknown, initialValue: T )
| 33 | } |
| 34 | |
| 35 | export function useResetKeyedState<T>( |
| 36 | resetKey: unknown, |
| 37 | initialValue: T |
| 38 | ): [T, Dispatch<SetStateAction<T>>] { |
| 39 | const [state, dispatch] = useReducer(resetKeyedStateReducer<T>, { |
| 40 | resetKey, |
| 41 | value: initialValue, |
| 42 | }); |
| 43 | |
| 44 | const setValue = useCallback<Dispatch<SetStateAction<T>>>((action) => { |
| 45 | dispatch({ type: 'set', action }); |
| 46 | }, []); |
| 47 | |
| 48 | if (!Object.is(state.resetKey, resetKey)) { |
| 49 | dispatch({ type: 'reset', resetKey, value: initialValue }); |
| 50 | return [initialValue, setValue]; |
| 51 | } |
| 52 | |
| 53 | return [state.value, setValue]; |
| 54 | } |
no outgoing calls
no test coverage detected