xxxxxxxxxx
useEffect(()=>{
// It will run everytime
})
// onMount
useEffect(()=>{
// It will run only one time
},[])
// component didUpdate
useEffect(()=>{
// it will run when dependency array change
},[data])
// component willUnmount
useEffect(()=>{
// for cleaning up the code
return(()=>{
})
})
xxxxxxxxxx
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => { document.title = `You clicked ${count} times`; });
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
import React, { useEffect } from 'react';
export const App: React.FC = () => {
useEffect(() => {
}, [/*Here can enter some value to call again the content inside useEffect*/])
return (
<div>Use Effect!</div>
);
}
xxxxxxxxxx
import { useState, useEffect } from 'react';
function App() {
const [pieceOfState, setPieceOfState] = useState(0);
useEffect(() => {
console.log('I\'m in useEffect!');
console.log('This will be called whenever an instance of this component mounts');
console.log('or whenever pieceOfState is updated');
}, [pieceOfState]);
return (
<div>
<button onClick={() => setPieceOfState(pieceOfState + 1)}>Click Me to Update pieceOfState!</button>
</div>
);
}
xxxxxxxxxx
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
xxxxxxxxxx
import React, { useState, useEffect } from 'react'
export default function App() {
const [count, setCount] = useState(0)
const color = count % 5 === 0 ? 'red' : 'blue'
useEffect(() => {
document.body.style.backgroundColor = color
}, [color])
return (
<button
onClick={() => {
setCount(count + 1)
}}
>
Click HERE to increment: {count}
</button>
)
}
xxxxxxxxxx
import { useEffect } from 'react';
import { createConnection } from './chat.js';
function ChatRoom({ roomId }) {
const [serverUrl, setServerUrl] = useState('https://localhost:1234');
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.connect();
return () => {
connection.disconnect();
};
}, [serverUrl, roomId]);
// ...
}
xxxxxxxxxx
import {useEffect, useState} from 'react';
export default function App() {
const [counter, setCounter] = useState(0);
// ✅ hook is called at top level (not conditionally)
useEffect(() => {
if (counter > 0) {
console.log('hello world');
}
});
return (
<div>
<button onClick={() => setCounter(counter + 1)}>toggle loading</button>
<h1>Hello world</h1>
</div>
);
}
xxxxxxxxxx
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
Source:reactjs.org
3
React useEffect, useState