32
loading...
This website collects cookies to deliver better user experience
React 16.8
. They let you use state and other React features without writing a class. It means you don't need to use a class component to use a state.useState
hook from react.
import React, { useState } from "react";
const [name, setName] = useState("Pratap");
name
: State Variable.setName
: Function to change the state value.useState
: useState react Hook.Pratap
: State initial value.import React, { useState } from "react";
const Example = () => {
const [name, setName] = useState("Pratap");
return (
<div>
<h1>{name}</h1>
</div>
);
};
export default Example;
Pratap
to Prasar
.import React, { useState } from "react";
const Example = () => {
const [name, setName] = useState("Pratap");
const changeState = () => {
//This will change the state value
setName("Prasar");
};
return (
<div>
<h1>{name}</h1>
<button onClick={changeState}>Change Name</button>
</div>
);
};
export default Example;