Answer the question
In order to leave comments, you need to log in
When to use useCallback, useMemo and useEffect?
What is the main difference between useCallback, useMemo and useEffect. Give examples of using useCallback, useMemo and useEffect.
Answer the question
In order to leave comments, you need to log in
useEffect is a hook that allows you to use a side effect. In classes, its counterpart was the use of componentDidMount, componentDidUpdate and componentWillUnmount. You can make subscriptions, send requests to manage animations, etc.
const [data, setData] = useState(null);
useEffect(() => {
const controller = new AbortController()
fetchData(controller.signal).then(setData)
return () => controller.abort()
}, [fetchData, setData])
const data = useMemo(() => array.map(mapper).filter(predicate).reduce(reducer), [array])
const handler = useCallback(() => {
// что-то сделать
}, [])
useEffect(() => {
handler(value)
// если не использовать useCallback, эффект будет срабатывать постоянно
}, [handler, value])
It is better to carefully study the doc, it is translated into Russian, otherwise you can miss the point
https://ru.reactjs.org/docs/hooks-effect.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question