πuseState in React
import { useState } from "react";
const App = () => {
// --------------------------------------------
// You can store any kind of JavaScript value in state. π
// const [initalValue, setInitalValue] = useState(0);
// const [initalValue, setInitalValue] = useState("HuXn");
// const [initalValue, setInitalValue] = useState(["one", "two", "three"]);
// const [initalValue, setInitalValue] = useState({
// one: "Alex",
// two: "John",
// three: "HuXn",
// });
// --------------------------------------------
const [counter, setCounter] = useState(0);
const increment = () => setCounter(counter + 1);
const decrement = () => setCounter(counter - 1);
return (
<section>
<h1>{counter}</h1>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
</section>
);
};
export default App;Last updated