Menu

Day 24 of My React Learning Journey πŸš€ | useState Hook Practice
πŸ“°

Day 24 of My React Learning Journey πŸš€ | useState Hook Practice

DEV Community: reactΒ·Saravanan CnΒ·1 day ago
#jx8ADby3
#devtoreact#devto#dev#usestate#value
Reading 0:00
15s threshold

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" ?…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.