Introduction to React JS
UseState with Examples
UseState allows you to have state variables in functional components. we can directly import or we can directly write it using React.hookName
Always write it inside the component or function
import React, { useState }from 'react' const App = () => { const [myName, setMyName] = useState("Parveen Barak"); const changeName = () => { let val = myName; if (val==="Parveen Barak"){ setMyName("Deepak Kumar") }else{ setMyName("Parveen Barak"); } } console.log(myName); return ( <div> <h1>{ myName }</h1> <button className="btn" onClick={changeName}>Click me</button> </div> ) } export default App -------------------------------------------------------------------------------------------------------------------------------------------------------------- Example - 2 import React, { useState } from 'react'; function Example() { // Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); }