Last active
August 22, 2020 02:16
-
-
Save diego-miranda-ng/30a4f8697d3e3b2b51348bf823af2293 to your computer and use it in GitHub Desktop.
Exemplo de utilização do useEffect para implementação do ciclo de vida do componente, executando os logs somente quando o estado referente a eles são atualizados.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
export default App = (props) => { | |
const [person, setPerson] = useState("TheAlfadur"); | |
const [count, setCount] = useState(0); | |
useEffect(() => { | |
console.log(count); | |
}, [count]); | |
useEffect(() => { | |
console.log(person); | |
}, [person]); | |
return ( | |
<> | |
<button onClick={() => setCount(count + 1)}> | |
INCREMENTAR O CONTADOR | |
</button> | |
<button | |
onClick={() => | |
setPerson(person === "TheAlfadur" ? "Diego" : "TheAlfadur") | |
} | |
> | |
ALTERAR NOME | |
</button> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment