Download react-use/src/useGetSetState.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 908 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useGetSetState.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useGetSetState.ts
-
curl -L -o useGetSetState.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useGetSetState.ts
908 Bytes
| import { useCallback, useRef } from 'react'; | |
| import useUpdate from './useUpdate'; | |
| const useGetSetState = <T extends object>( | |
| initialState: T = {} as T | |
| ): [() => T, (patch: Partial<T>) => void] => { | |
| if (process.env.NODE_ENV !== 'production') { | |
| if (typeof initialState !== 'object') { | |
| console.error('useGetSetState initial state must be an object.'); | |
| } | |
| } | |
| const update = useUpdate(); | |
| const state = useRef<T>({ ...(initialState as object) } as T); | |
| const get = useCallback(() => state.current, []); | |
| const set = useCallback((patch: Partial<T>) => { | |
| if (!patch) { | |
| return; | |
| } | |
| if (process.env.NODE_ENV !== 'production') { | |
| if (typeof patch !== 'object') { | |
| console.error('useGetSetState setter patch must be an object.'); | |
| } | |
| } | |
| Object.assign(state.current, patch); | |
| update(); | |
| }, []); | |
| return [get, set]; | |
| }; | |
| export default useGetSetState; | |