-
-
Save nympheastudio/4ebdae1eee04f1c5f87802d56545fb46 to your computer and use it in GitHub Desktop.
recoil with useRef
This file contains 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