Skip to content

Instantly share code, notes, and snippets.

@joboccara
Created April 23, 2021 07:21
Show Gist options
  • Save joboccara/23bc56a4a63a06ff5d5749575d78761b to your computer and use it in GitHub Desktop.
Save joboccara/23bc56a4a63a06ff5d5749575d78761b to your computer and use it in GitHub Desktop.
const CountContext = React.createContext(null);
const CountContextProvider = ({ children }) => {
const [count, setCount] = React.useState(0);
const countRef = React.useRef(count); // more on this in the next section
countRef.current = count;
const onClickMemo = React.useCallback(
() => window.alert(`clickMemo, count: ${countRef.current}`),
[]
);
return (
<CountContext.Provider value={{ count, setCount }}>
<GrandParent>{children}</GrandParent>
<MemoComponent onClick={onClickMemo} />
</CountContext.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment