Monday 15 August 2022

ReactJS useState() method explained

 The ReactJS useState() method is used to save and set state variables in the React Components.

For this to use, we need to import useState() from React.

The useState() method returns an array.

It has two parameters:

1) state variable

2) function to change state variable.


For example:


const [count, setCount] = useState(0);

Here we are destructing the useState() array with two variables.

the count is variable and setCount is a method.

// Import useState from React

import {useState} from 'react';


function App() {

 const [count, setCount] = useState(0);

}


We can set the count using setCount() method.

No comments:

Post a Comment

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...