What is the useState() hooks in React JS | How to use useState() hook in React JS

Hooks are very important in react js and the most common hook is the useState Hook.

In this blog, we will look into the functionality of this hook and we will understand how this hook work.

useState() Hook

useState hook is used to manage dynamic values of variables, basically, it is used to manage state.

So if you want to change the value of a variable on a button click or any other events we use the useState hook.

Let’s understand with an example.

In our app.js file, we are adding an input field and button, On click of this button, we are changing the value of the inputText variable, which we have already declared in our app function.

import style from './App.module.css';
function App() {
  let inputText = 'Simple Variable';
  let submitValue = () =>{
    inputText='Variable Change';
    console.log(inputText);
  }
  return (
    <div>
      <input type="text" value={inputText}/>
      <button onClick={submitValue}>Click</button>
    </div>
  );
}
export default App;

In this method or function, we are changing the value of the inputText variable, On button click, we are calling the submitValue function which is updating the value of the inputText variable and outputting inside the console.

So according to normal javascript, it should be changed and updated on the input field value as well, but what is happening is, it is updating in the console but not updating in the input field value, react js doesn’t work like this, we have to manage its state to update it dynamically on input filed value as well.

Let’s see the output for the above code.

In the above output, you can see on the click of the button input field value didn’t change, but in the console, it gets updated.

So this is the problem with normal javascript variables in react js, to manage these states in react app we use the useState hook.

Now let’s see what happens when we use the useState hook in the same example.

So first we need to import the useState hook inside our App.js file then we can assign this inputText variable to useState hook.

import { useState } from 'react';
import style from './App.module.css';
function App() {
  let [inputText,setInputText] = useState('Simple Variable');
  let submitValue = () =>{
    setInputText('Variable Changed');
    console.log(inputText);
  }
  return (
    <div>
      <input type="text" value={inputText}/>
      <button onClick={submitValue}>Click</button>
    </div>
  );
}
export default App;

As you can see now, we are declaring the inputText variable and using the useState hook to manage its state, setInputText is a function that is responsible to change the state of the inputText variable, so inside the submitValue function, we are setting the setInputText value to “Variable Changed”, which automatically assign it to inputText variable and will reflect the output in console and input field value as well.

Output for the above code is shown below

If you like this blog please comment and share.

Related Posts

angular vs react-min

What is the difference between Angular & React JS, Which one you should choose?

Angular & React JS both are use for Frontend Development, To make Single page applications or Web Apps. Here is a quick difference between both of them….

react js icon

What is Redux? What is action, reducer and store in Redux

In this blog we will discus about the redux, what is Redux and how you can use it in React application. What is Redux? Redux is a…

react js icon

What is useEffect Hook? How to Fetch data from API using useEffect

useEffect hook is used to manage side effects that are not related to component rendering. Mostly useEffect hook is used for managing console logs, animations, loading data,…

react js icon

What is useReducer Hook? What is the difference between useState and useReducer Hooks?

useReudcer hook takes the current state and returns a new state, it is used for state management. With useReducer, we can directly call the function inside the…

react js icon

Creating React JS App without using npx create-react-app command

It’s easy to create a react application by just running npx create-react-app project_name command, do you know what are the basic requirements for creating a react application…

react js icon

How to use Style Module in React JS

This blog going to be the shortest one, because this is an easy topic but very important. So I decided to write a blog on this. Basically…

Leave a Reply

Your email address will not be published. Required fields are marked *