I built a simple counter app in ReactJS. Code is below.
import React, { useState } from "react";
import "./styles.css";
export default function App() {
const [countNum, setCountNum] = useState(0);
function increaseCount() {
setCountNum(countNum + 1);
}
function decreaseCount() {
if (countNum > 0) {
setCountNum(countNum - 1);
}
}
function disableChecker() {
if (countNum === 0) {
return true;
} else {
return false;
}
}
return (
<div className="App">
<button onClick={() => decreaseCount()} disabled={disableChecker()}>Decrease</button>
<button onClick={() => increaseCount()}>Increase</button>
<h2>{countNum}</h2>
</div>
);
}
I just want to know why does onClick={() => increaseCount()} works AND why onClick={increaseCount()} or onClick={() => increaseCount} doesn't work?
A beginner here, please guide me to the answer.
disabled={countNum === 0}would be enough, there's no need for a function.()following the function name.