How to Create State and Use in React JS?

Oct 21, 2022 . Admin

Hello Friends,

This article will give you an example of how to create a state and use it in react js. We will look at the example of create state and use in react js. this example will help you state and use in react js. We will look at an example of react js state. You just need to some step to done create a state in react js.

In this example how to create a state and use it in react js. I will simply explain how to make a state and use react js. The state is a built-in React object that is used to contain data or information about the component.

Step 1: Create React JS App

In this step, open your terminal and execute the following command on your terminal to create a new react app:

npx create-react-app reactjs-my-example
Step 2: Create index.js file src/index.js
import React, { useState } from 'react'
import ReactDOM from 'react-dom/client';

function Usestate() {

    //create state
    const [name,setName] = useState('Mywebtuts.com');

    //get state value
    return (
        <div>
            <h1 className="Dialog-title">
                {name}
            </h1>
        </div>
    );
}
  
const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(<Usestate />);
Run React JS App:
npm start

Output:

Mywebtuts.com

I hope it can help you...

#React JS