Skip to content

Instantly share code, notes, and snippets.

@KevinVR
Created February 26, 2021 11:16
Show Gist options
  • Save KevinVR/7075d3b59ed6d34a15b7f6a342c10e38 to your computer and use it in GitHub Desktop.
Save KevinVR/7075d3b59ed6d34a15b7f6a342c10e38 to your computer and use it in GitHub Desktop.
import React from 'react';
const ClickCounter = ({initialClicks}) => {
// If initialClicks is not defined, use 0 as default
const [clicks, setClicks] = useState(initialClicks || 0);
// Implement an onClick method for the button
const incrementClicks = () => {
setClicks(clicks + 1);
};
// Use React.Fragment to avoid extra divs
return <>
<h1>Click Counter</h1>
<p>Amount of clicks: {clicks}</p>
<button onClick={incrementClicks}>Click me!</button>
</>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment