0
0
Reactframework~30 mins

Updating state in React - Mini Project: Build & Apply

Choose your learning style9 modes available
Updating State in React
📖 Scenario: You are building a simple React component that tracks how many times a button is clicked. This is like counting how many times you press a button on a game controller.
🎯 Goal: Create a React functional component that shows a number starting at zero and a button. Each time the button is clicked, the number increases by one.
📋 What You'll Learn
Use React functional components
Use the useState hook to create and update state
Display the current count in a <div>
Add a button that updates the count when clicked
💡 Why This Matters
🌍 Real World
Counting clicks or user interactions is common in web apps, like tracking likes, votes, or steps in a process.
💼 Career
Understanding how to update state in React is essential for building interactive user interfaces in modern web development jobs.
Progress0 / 4 steps
1
Set up initial state with useState
Create a React functional component called ClickCounter. Inside it, use the useState hook to create a state variable called count with the initial value 0. Also create the state updater function called setCount.
React
Need a hint?

Remember to import useState from React. Use array destructuring to get count and setCount from useState(0).

2
Add a button to update the count
Inside the ClickCounter component, add a <button> element. Set its onClick attribute to a function that calls setCount(count + 1) to increase the count by one when clicked.
React
Need a hint?

Use an arrow function inside onClick to call setCount(count + 1).

3
Display the current count
Modify the ClickCounter component to display the current count inside a <div> above the button. Show the text exactly as Count: {count}.
React
Need a hint?

Use curly braces {count} inside JSX to show the current count.

4
Add accessibility and export the component
Add an aria-label attribute to the <button> with the value Increment count for accessibility. Then export the ClickCounter component as the default export.
React
Need a hint?

Use aria-label on the button for screen readers. Use export default ClickCounter; to export.