xxxxxxxxxx
useEffect(() => {
fetch(`https://jsonplaceholder.typicode.com/${query}`)
.then(response => response.json())
.then(json => setData(json));
}, [query]); // only re-run on query change
xxxxxxxxxx
const App = (props) => {
const [change, setChange] = useState(false);
useEffect(() => {
return () => console.log("CHANGED!");
}, [change]);
return (
<div>
<button onClick={() => setChange(true)}>CLICK</button>
</div>
);
};
xxxxxxxxxx
useEffect(() => {
// send HTTP request
// save response to variable
}, [])
xxxxxxxxxx
What does useEffect do? By using this Hook, you tell React that
your component needs to do something after render.
React will remember the function you passed (we’ll refer to it
as our “effect”), and call it later after performing the DOM updates.