The useState is a react hook that holds a state of the component.
In this case, the state is a counter.The + button increases the state of the counter. The - button decreases the counter.
・src/Example.js
import { useState } from "react";
const Example = () => {
const [count, setCount] = useState(0);
return (
<>
<CountResult title="count" count={count} />
<CountUpdate setCount={setCount} />
</>
);
};
const CountResult = ({ title, count }) => (
<h3>
{title} : {count}
</h3>
);
const CountUpdate = ({ setCount }) => {
const countUp = () => {
setCount((prev) => prev + 1);
};
const countDown = () => {
setCount((prev) => prev - 1);
};
return (
<>
<button onClick={countUp}>+</button>
<button onClick={countDown}>-</button>
</>
);
};
export default Example;
・Here is the action of countup.
・Here is the action of countdown.
Top comments (0)