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 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 { 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
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";
const App = () => {
const [color, setColor] = useState("black");
useEffect(() => {
const changeColorOnClick = () => {
if (color === "black") {
setColor("red");
} else {
setColor("black");
}
};
document.addEventListener("click", changeColorOnClick);
return () => {
document.removeEventListener("click", changeColorOnClick);
};
}, [color]);
return (
<div>
<div
id="myDiv"
style={{
color: "white",
width: "100px",
height: "100px",
position: "absolute",
left: "50%",
top: "50%",
backgroundColor: color,
}}
>
This div can change color. Click on me!
</div>
</div>
);
};
export default App;
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