Skip to content

Instantly share code, notes, and snippets.

@jickoo
Created March 23, 2021 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jickoo/71704a2f59e94040f0d790da1be631f4 to your computer and use it in GitHub Desktop.
Save jickoo/71704a2f59e94040f0d790da1be631f4 to your computer and use it in GitHub Desktop.
recoil with useRef
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