

React中useMemo与useCallback的区别 - 小提莫~
source link: https://www.cnblogs.com/jiajialove/p/16595701.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

useMemo
把“创建”函数和依赖项数组作为参数传⼊入useMemo,它仅会在某个依赖项改变时才重新计算memoized 值。这种优化有助于避免在每次渲染时都进⾏行行⾼高开销的计算。
importReact, { useState, useMemo } from"react"; export default functionUseMemoPage(props) { const [count, setCount] =useState(0); constexpensive=useMemo(() => { console.log("compute"); let sum=0; for (leti=0; i<count; i++) { sum+=i; } return sum;//只有count变化,这⾥里里才重新执⾏行行 }, [count]); const [value, setValue] =useState(""); return (<div><h3>UseMemoPage</h3> <p>expensive:{expensive}</p><p>{count}</p>
<button onClick={() =>setCount(count+1)}>add</button>
<input value={value} onChange={event=>setValue(event.target.value)} /></div> ); }
useCallback
把内联回调函数及依赖项数组作为参数传⼊入useCallback,它将返回该回调函数的 memoized 版本,该回调函数仅在某个依赖项改变时才会更更新。当你把回调函数传递给经过优化的并使⽤用引⽤用相等性去避免⾮非必要渲染(例例如shouldComponentUpdate)的⼦子组件时,它将⾮非常有⽤用
importReact, { useState, useCallback, PureComponent } from"react"; export default function UseCallbackPage(props) { const [count, setCount] =useState(0); const addClick=useCallback(() => { let sum=0; for (leti=0; i<count; i++) { sum+=i; } return sum; }, [count]); const [value, setValue] =useState(""); return ( <div><h3>UseCallbackPage</h3>
<p>{count}</p>
<buttononClick={() =>setCount(count+1)}>add</button>
<inputvalue={value} onChange={event=>setValue(event.target.value)} />
<ChildaddClick={addClick} /></div> );
}
class ChildextendsPureComponent {
render() {
console.log("child render");
const { addClick } =this.props;
return (
<div><h3>Child</h3>
<buttononClick={() =>console.log(addClick())}>add</button></div>
)
}
}
useCallback(fn, deps)相当于useMemo(() => fn, deps)。
注意依赖项数组不不会作为参数传给“创建”函数。虽然从概念上来说它表现为:所有“创建”函数中引⽤用的值都应该出现在依赖项数组中。未来编译器器会更更加智能,届时⾃自动创建数组将成为可能。
Recommend
-
54
【译】什么时候使用 useMemo 和 useCallback 2019-08-19 译文...
-
18
Lets, check react advanced hooks, i.e. UseCallback , UseMemo , UseRef , and UseContext . All these come under React 16.8 version and help t...
-
5
The Difference Between useMemo and useCallback — An In-Depth LookSeptember 24th 2021 5 A useMemo...
-
8
React-Hooks: What is The Difference Between useCallback And useMemo?October 13th 2021 new story8React...
-
8
Charles 🛰 Posted on Feb 7...
-
7
IntroductionIf you've struggled to make sense of useMemo and useCallback, you're not alone! I've spoken w...
-
8
When to use the two hooks - useCallback and useMemo? Sep 22, 2022 , by Onkar Hasabe 3 minute read Every...
-
8
本篇文章我们主要了解下 useCallback 和 useMemo 是如何来优化React组件的。 我们解析的源码是 React18.1.0 版本,请注意版本号。React 源码学习的 GitHub 仓库地址:
-
8
How does useMemo and useCallback work ? When render a component, React will process the whole Component code. There might be several computed values and functions that its values only depends on f...
-
7
useEffect 先理解 useEffect 有助于学习 useMemo 和 useCallback。因为...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK