Click counter
React
Easy
Objective
Create a simple React component that counts and displays the number of times a button has been clicked.
Requirements
The component should be named
ClickCounter
It should render a button that, when clicked, updates a count
It should display the current count
Evaluation Criteria
If I gave you this test, here's what I'd be looking for:
Use of state
You should be using the
useState
hook to count the number of clicks
Click to reveal
Solution
Here's how I would approach this, but keep in mind there might be other ways to solve this problem.
import React, { useState } from 'react';
function ClickCounter () {
const [count, setCount] = useState(0);
return (
<div>
Current count: {count}
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default ClickCounter;
Click to reveal