Click counter

React
Easy

Objective

Create a simple React component that counts and displays the number of times a button has been clicked.

Requirements

  1. The component should be named ClickCounter

  2. It should render a button that, when clicked, updates a count

  3. It should display the current count

Evaluation Criteria

If I gave you this test, here's what I'd be looking for:

  1. Use of state

    1. 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