Skip to content

Instantly share code, notes, and snippets.

@megatroom
Created Feb 13, 2020
Embed
What would you like to do?
React Counter with nested function as side effect dependency [solution 1]
const Logger = ({ index, getCounter }) => {
const [text, setText] = useState("");
// Executa a função para obter o contador
const counter = getCounter();
useEffect(() => {
// usa o contador dentro do callback
const newText = `Contador ${index}: ${counter}`;
setText(newText);
console.log(newText);
}, [
index,
counter // usa o contador como dependência
]);
return <p>{text}</p>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment