Skip to content

Instantly share code, notes, and snippets.

@tacigar
Created August 22, 2021 06:31
Show Gist options
  • Save tacigar/1ce0f0619d419bb743c77891ccf87375 to your computer and use it in GitHub Desktop.
Save tacigar/1ce0f0619d419bb743c77891ccf87375 to your computer and use it in GitHub Desktop.
import { useCallback, useState, useEffect } from "react";
const X = () => {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
setCount((prev) => prev + 1);
}, []);
return <button onClick={handleClick}>Button1 ({count})</button>;
};
const Y = ({ children }) => {
useEffect(() => {
console.log("children are updated");
}, [children]);
return <div>{children}</div>;
};
function App() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
setCount((prev) => prev + 1);
}, []);
return (
<div>
<button onClick={handleClick}>Button2 ({count})</button>
<Y>
<X />
</Y>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment