React Hooks | Get Time on refreshing and Clicking button using useState Hook
React Hooks | Get Time on refreshing and Clicking button using useState Hook
App.js
import { useState } from 'react';import './App.css';const App = () => {let CurrentTime = new Date().toLocaleTimeString();const [btn, setBtn] = useState(CurrentTime);const btnClick = () => {let currentTime = new Date().toLocaleTimeString();;setBtn(currentTime)}return (<><h1>{btn}</h1><button onClick={btnClick}>Click me</button></>);}export default App;
index.css
*{box-sizing: border-box;padding: 0;margin: 0;background-color: #d2dbdd;}div{width: 100%;height: 100vh;display: flex;flex-direction: column;justify-content: center;align-items: center;}button{padding: 15px 20px;background: #9b59b6;color: white;border: 2px solid #ecf0f1;text-transform: uppercase;cursor: pointer;border-radius: 20px;}
Live update Time using Hooks in ReactJs
App.js
import { useState } from 'react';import './App.css';const App = () => {let CurrentTime = new Date().toLocaleTimeString();const [btn, setBtn] = useState(CurrentTime);const btnClick = () => {let currentTime = new Date().toLocaleTimeString();;setBtn(currentTime)}setInterval(btnClick,1000);return (<><h1>{btn}</h1>{/* <button onClick={btnClick}>Click me</button> */}</>);}export default App;
Post a Comment