Hook and useState in React
import './App.css';
import React ,{useState} from "react";
function App(){
//setInterval(updateTime,1000) //1000 is sec
//this is a function by javascript
const now = new Date().toLocaleTimeString();
const [time, setTime] = useState(now); // first wla variable and 2nd wala function
ever time useState me kuch hona chiya phela
function updateTime(){
const newTime = new Date().toLocaleTimeString(); //variable
setTime(newTime);
}
return(
<div className = "container">
<h1>{time}</h1>
<button onClick={updateTime}>GetTime</button>
</div>
)
}
export default App;
/////////////////////////////////////////////////////////////////////////////////////
Step 1 :- we have to Import useSate from react
step 2 ;- creat function and under which create another function
create aka variable const and [] first wla hoga ake variable and 2nd wala mae function
hoga
Comments
Post a Comment