Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
export function useCustomThing() {
// Assume this changes as the user interacts with the app
// (Note: this code sample doesn't show setState being called, but that
// part doesn't matter)
const [state, setState] = useState();
const stateRef = useRef(state);
useEffect(() => {
stateRef.current = state;
}, [state]);
return {
logLatestState() {
// A: use the state
console.log('The latest state is:', state);
// B: use the stateRef
console.log('The latest state is:', stateRef);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.