Created
June 18, 2022 08:49
-
-
Save mhaecal/06d13cca2b73c30c0a568c92c46b4cb6 to your computer and use it in GitHub Desktop.
onClick Child Component to Fire a function in Parent Component (onClink Props React)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { useState } = React; | |
function PageComponent() { | |
const [count, setCount] = useState(0); | |
const increment = () => { | |
setCount(count + 1) | |
} | |
return ( | |
<div className="App"> | |
<ChildComponent onClick={increment} count={count} /> | |
<h2>count {count}</h2> | |
(count should be updated from child) | |
</div> | |
); | |
} | |
const ChildComponent = ({ onClick, count }) => { | |
return ( | |
<button onClick={onClick}> | |
Click me {count} | |
</button> | |
) | |
}; | |
ReactDOM.render(<PageComponent />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment