useeffect and usestate
Replace this code in App.js for useeffect demo code
import React, { useState, useEffect } from 'react';
function Counter() {
// Define a state variable "count" and a function "setCount" to update it.
const [count, setCount] = useState(0);
// useEffect is used for side effects in function components.
// In this case, we want to update the document title when "count" changes.
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]);
return (
<div>
<h1>Counter Example</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(count - 1)}>Decrement</button>
</div>
);
}
export default Counter;
Replace this code in App.js for usestate demo code
import logo from './logo.svg';
import './App.css';
import React, {useState} from 'react';
function App() {
console.log(useState('Hello World'));
const[myName,setNewName]=useState('first name');
const changeName=()=>
{
setNewName('second name');
}
return (
<div>
<h1>{myName}</h1>
<button onClick={changeName}>Click Me Please</button>
</div>
);
}
export default App;
Comments
Post a Comment