Created
March 23, 2021 18:26
-
-
Save jickoo/71704a2f59e94040f0d790da1be631f4 to your computer and use it in GitHub Desktop.
recoil with useRef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from 'react'; | |
import { useRecoilState } from 'recoil'; | |
import { someAtom } from './recoilstates/someState'; | |
const yourCustomHook = () => { | |
const [someState, setSomeState] = useRecoilState(someAtom); | |
const latestSomeState = useRef(someState); | |
useEffect(()=> { | |
latestSomeState.current = someState; | |
},[someState]); | |
// I use useEffect here as an example, | |
// custom hook can contain any React hooks | |
useEffect(()=>{ | |
// your custom hook logic here | |
// use latestSomeState.current for latest value | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment