This file contains 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 { useCallback } from "react"; | |
export default function Parent() { | |
const doSomething = useCallback(() => { | |
// A função sempre terá a mesma referência. | |
}, []); | |
const doAnotherThingWithAandB = useCallback(() => { | |
// Sua referência irá mudar quando a e b mudarem. |
This file contains 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 { useMemo } from "react"; | |
export default function Component() { | |
const value = useMemo(() => { | |
// Faz algo complexo e demorado. | |
// Se x e y já tiverem sido usados, irá retornar o valor anterior memorizado. | |
}, [x, y]); | |
} |
This file contains 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
function reducer(state, action) { | |
switch (action.type) { | |
case "increment": | |
return state + 1; | |
case "decrement": | |
return state - 1; | |
default: | |
return state; | |
} | |
} |
This file contains 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 { useRef, useEffect, forwardRef } from "react"; | |
export default function Parent() { | |
const childRef = useRef(null); | |
return <Text ref={childRef} />; | |
} | |
const Text = forwardRef((props, ref) => { | |
return <p ref={ref}>Exemplo de texto</p>; |
This file contains 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 { useRef, useEffect, useRef, useState } from "react"; | |
export default function Form() { | |
const [text, setText] = useState(""); | |
const timeToCallSomething = useRef(null); | |
const fetchSomething = () => { | |
// Realiza alguma requisição http. | |
}; |
This file contains 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
const time = useRef(0); | |
console.log(time.current); // 0; |
This file contains 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
useEffect(() => { | |
// Executa a função logo antes de desmontar o componente. | |
return () => doSomething(); | |
}, []); |
This file contains 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
useEffect(() => { | |
// Faz algo com x atualizado. | |
}, [x]); |
This file contains 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
useEffect(fn, []); |
This file contains 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 { useEffect } from "react"; | |
export default function Component() { | |
const [filters, setFilters] = useState([]); | |
const fetchSomething = (params) => {}; | |
const onResizeScreenHandler = () => {}; | |
// Observa mudanças no estate filters. | |
useEffect(() => { |