Skip to content

Instantly share code, notes, and snippets.

@nympheastudio
Forked from jickoo/recoilWithUseRef.js
Created June 9, 2023 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nympheastudio/4ebdae1eee04f1c5f87802d56545fb46 to your computer and use it in GitHub Desktop.
Save nympheastudio/4ebdae1eee04f1c5f87802d56545fb46 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