When I click the button both components are decremented at the same rate. I would like them to decrement by different values.
I am just learning React not sure if it is possible.
This is what I have so far.
const ParentComponent = () => {
const [num, setNum] = useState(100);
const handleClick = () => {
if (num === 0) {
return;
}
const decrement = Math.floor(Math.random() * 20) + 1;
setNum(() => Math.max(num - decrement, 0));
};
return (
<div>
<ChildComponent value={num} />
<ChildComponent value={num} />
<button onClick={handleClick}>Random Decrement</button>
</div>
);
};
const ChildComponent = ({ value }) => <div>Num: {value}</div>;