1 - Simple Counter App
Description
Create a React application that displays a simple counter. The counter should initially display 0 and have two buttons to increment or decrement the count. The count should update accordingly when a button is clicked.
Algorithm
Initialize a state variable to store the count, set to 0.
Render the current count to the screen.
Create two buttons for incrementing and decrementing the count.
Define functions to update the state variable when a button is clicked.
Attach these functions to the respective button's onClick event.
Classes
Counter
: A React component that manages the count state and renders the count and buttons.
Code
Explanation
The code defines a Counter
component that utilizes the useState
hook to initialize a state variable count
to 0. Two functions, handleIncrement
and handleDecrement
, are defined to update the count
state variable. The component renders the current count
and two buttons. The handleIncrement
and handleDecrement
functions are attached to the respective button's onClick
event, enabling the updating of the count when a button is clicked.
Further Possible Enhancements
Implement a limit to the count (e.g., prevent it from going below 0 or above a certain number).
Display the count history or a list of previous counts.
Allow users to input a custom increment or decrement value.
Add a reset button to set the count back to 0.
Last updated