Today, I learned more about the useState Hook in React. I practiced increment, decrement, and reset operations. I also worked on two scenarios: a toggle button and an input field.
1. useState Hook
The useState Hook allows us to store and update data in a functional component.
Syntax
const [state, setState] = useState(initialValue);
state stores the current value.
setState updates the value.
initialValue is the starting value.
I also learned that component names should start with a capital letter, such as ToggleButton or NameInput.
A functional component is a JavaScript function that returns JSX.
2. Toggle Button: ON and OFF
I created a button that switches between ON and OFF.
`import { useState } from "react";
function Toggle() {
const [ toggle,settoggle] = useState("OFF");
function togglebtn(){ if(toggle === "OFF"){ settoggle("ON"); } else{ settoggle("OFF"); }
} // toggle === "OFF" ?β¦