Hooks useEffect
Lo useEffect ti consente di sincronizzare un componente con un sistema esterno (asincrono)
Esempio di un possibile utilizzo
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
const App = () => {
//DICHIARAZIONI DEGLI useState
const [contatore, setContatore] = useState(0);
const [calcolo, setCalcolo] = useState(0);
//EFFETUO IL CALCOLO CON UNO useEffect
useEffect(() => {
setCalcolo(() => contatore * 2);
}, [contatore]);
//RETURN --> Stampa a video
return (
<div>
{/* STAMPO IL CONTATORE */}
<p>Contatoree: {contatore}</p>
{/* STAMPO IL CALCOLO (contatore x 2) */}
<p>Calcolo: {calcolo}</p>
{/* BUTTON PER INCREMENTARE IL CONTATORE */}
<button onClick={() => setContatore((c) => c + 1)}>+</button>
</div>
);
}