State in React is used to store information, it is an object of a component. The state holds data that may change during the lifetime of a React component.
We can use state in React JS we can use as,
import React from "react"; const state = React.useState();
or
import React, {useState} from "react";
const state = useState();
React State Example Code
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App"
ReactDOM.render(
<App/>,
document.getElementById("root")
);
components/App.jsx
import React, {useState} from "react";
function App() {
const [count, setCount] = useState(0);
function increase(){
setCount(count+1);
}
function decrease(){
setCount(count-1);
}
return (
<div className="container">
<h1>{count}</h1>
<button onClick={increase}>+</button>
<button onClick={decrease}>-</button>
</div>);
}
export default App;