Goal: Create a simple React counter using hooks and event handling.
To create a counter you will need:
useState hook to store the current counter value.// Example of using useState
const [count, setCount] = useState(0);
// Example event handler
const increment = () => {
setCount(count + 1);
};import React, { useState } from 'react';
export default function Counter() {
// Initialize counter state with initial value 0
const [count, setCount] = useState(0);
// Function to increment the counter value
const increment = () => {
setCount(count + 1);
};
// Function to decrement the counter value, but not below 0
const decrement = () => {
if (count > 0) {
setCount(count - 1);
}
};
return (
<div style={{ textAlign: 'center', margin: '20px' }}>
<h2>Counter</h2>
<div style={{ fontSize: '24px', margin: '10px' }}>{count}</div>
<div>
<button
onClick={increment}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: '#4CAF50',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
+
</button>
<button
onClick={decrement}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: '#f44336',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
-
</button>
</div>
</div>
);
}Solution Analysis:
useState hook to create the count state with an initial value of 0.increment and decrement functions to change the counter value.decrement function, we check that the counter value is greater than 0 so as not to decrease it below zero.You need to create a simple React counter component. The component should display the current counter value and provide buttons to increment and decrement it.
Key requirements:
// Basic counter component example
<Counter /> // Displays 0 and + and - buttons
// After clicking the + button
// Displays 1 and + and - buttons
// After clicking the - button
// Displays 0 and + and - buttonsCounteruseState hook to manage stateGoal: Create a simple React counter using hooks and event handling.
To create a counter you will need:
useState hook to store the current counter value.// Example of using useState
const [count, setCount] = useState(0);
// Example event handler
const increment = () => {
setCount(count + 1);
};import React, { useState } from 'react';
export default function Counter() {
// Initialize counter state with initial value 0
const [count, setCount] = useState(0);
// Function to increment the counter value
const increment = () => {
setCount(count + 1);
};
// Function to decrement the counter value, but not below 0
const decrement = () => {
if (count > 0) {
setCount(count - 1);
}
};
return (
<div style={{ textAlign: 'center', margin: '20px' }}>
<h2>Counter</h2>
<div style={{ fontSize: '24px', margin: '10px' }}>{count}</div>
<div>
<button
onClick={increment}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: '#4CAF50',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
+
</button>
<button
onClick={decrement}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: '#f44336',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
-
</button>
</div>
</div>
);
}Solution Analysis:
useState hook to create the count state with an initial value of 0.increment and decrement functions to change the counter value.decrement function, we check that the counter value is greater than 0 so as not to decrease it below zero.You need to create a simple React counter component. The component should display the current counter value and provide buttons to increment and decrement it.
Key requirements:
// Basic counter component example
<Counter /> // Displays 0 and + and - buttons
// After clicking the + button
// Displays 1 and + and - buttons
// After clicking the - button
// Displays 0 and + and - buttonsCounteruseState hook to manage stateThe code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!