I'm trying to make a clicker game in react. I have a component that increments the amount of coins by 1. However, I want to be able to send this to another component so it can be used. Example: purchasing an upgrade, and subtracting the amount of coins.
How can I do this?
Game.jsx
export default function Game() {
// set state to bet that of the Counter when it is updated
const element = (
<div className="app">
<Counter />
<UpgradeMenu />
</div>
);
return element;
}
Counter.jsx
export default function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
const element = (
<div className="section">
<div className="counter">
<h1>{count} coins</h1>
<button onClick={increment}>Click</button>
</div>
</div>
);
return element;
}
Thank you in advance, and I hope this makes sense what I'm trying to do.