example 1.4
Wed May 15 2024 10:22:12 GMT+0000 (Coordinated Universal Time)
Saved by
@beyza
import { useState } from 'react';
// Custom hook to manage a counter
function useCounter(initialValue = 0) {
const [count, setCount] = useState(initialValue);
// Function to increment the count
const increment = () => {
setCount(count + 1);
};
// Function to decrement the count
const decrement = () => {
setCount(count - 1);
};
// Return the count value and functions to update it
return {
count,
increment,
decrement
};
}
// Example usage:
function Counter() {
// Use the custom hook to manage the counter
const { count, increment, decrement } = useCounter(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}
content_copyCOPY
https://chatgpt.com/c/3e4f5a44-577a-4ca4-970d-80b8a2a76efa
Comments