diff options
| author | Haishan <[email protected]> | 2020-07-01 22:06:26 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2020-07-04 17:58:56 +0800 |
| commit | 32bed273c83f0593187110d2b08a0f9ec5a7efd7 (patch) | |
| tree | 0b47da752de3ee0d87945c1122b2cf9d3bf8043f /src/hooks/useTextInput.ts | |
| parent | 55e928a87f561ab927774834b50e099a0758522d (diff) | |
feat: support rule provider
Diffstat (limited to 'src/hooks/useTextInput.ts')
| -rw-r--r-- | src/hooks/useTextInput.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/hooks/useTextInput.ts b/src/hooks/useTextInput.ts new file mode 100644 index 0000000..1fa19f7 --- /dev/null +++ b/src/hooks/useTextInput.ts @@ -0,0 +1,23 @@ +import debounce from 'lodash-es/debounce'; +import * as React from 'react'; +import { RecoilState, useRecoilState } from 'recoil'; + +const { useCallback, useState, useMemo } = React; + +export function useTextInut( + x: RecoilState<string> +): [(e: React.ChangeEvent<HTMLInputElement>) => void, string] { + const [, setTextGlobal] = useRecoilState(x); + const [text, setText] = useState(''); + const setTextDebounced = useMemo(() => debounce(setTextGlobal, 300), [ + setTextGlobal, + ]); + const onChange = useCallback( + (e: React.ChangeEvent<HTMLInputElement>) => { + setText(e.target.value); + setTextDebounced(e.target.value); + }, + [setTextDebounced] + ); + return [onChange, text]; +} |
