Skip to content

Instantly share code, notes, and snippets.

@mhaecal
Created June 18, 2022 08:49
Show Gist options
  • Save mhaecal/06d13cca2b73c30c0a568c92c46b4cb6 to your computer and use it in GitHub Desktop.
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)
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