Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 10, 2021 06:05 pm GMT

React useState

Hey guys this is my first blog...

Alt Text
Suppose we have to render name in react app . We can save that name in simple variable and render that name .

Alt Text
But what if we want to change name to Alfred after clicking Change Name button , so let's pass changeName function . In changeName function if we change value of variable name to Alfred that's not going to work.

So here we use useState hook .
for using useState hook first we need to import it from react.
Example-
1.import { useState } from "react";

With help of useState we set two variables like
Alt Text

Here name variable represents Bruce , after that in changeName function we use setName variable to set name to Alfred.

Alt Text
now after click name will change to Alfred. We can pass different type of data in useState hook.


Original Link: https://dev.to/abhisheks12/react-usestate-4o71

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To