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
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, { useEffect, useState } from 'react';
const [count,setCount] = useState(0);
//The effect runs after each render since no dependencies are specified.
//run each render
useEffect(() => {
console.log("no depencies, no test");
})
//The effect runs once after the initial render due to the empty dependency array.
//only run start
useEffect(() => {
console.log("include depencies, no test");
},[])
//The effect runs after the initial render and whenever the count state variable changes.
//run start render and when changing count
useEffect(() => {
console.log("include depencies and test");
},[count])
return(
<div classsName="container">
<h2>{count}</h2>
<button onClick={() => setCount(count + 1)} > click </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(()=>{
})
})