[React] Always useMemo your context value
Have a similar post about Reac.memo. This blog is the take away from this post.
To understand why to use 'React.useMemo' or 'React.memo' (basiclly lastest React version use 'useMemo'), is that if the function component or context create a new reference, you need to use memo.
Context:
Context provider always create a new object to the comsuers. So use memo everytime you can.
function CountProvider(props) {
const [count, setCount] = React.useState(0)
const value = React.useMemo(() => {
return {
count,
setCount,
}
}, [count])
return <CountContext.Provider value={value} {...props} />
}
function component:
Check the props, only do the re-render when props changes.
const TodoItem = React.memo(({ todo, onChange, onDelete }) => {
console.log("TodoItem5", { todo, onChange, onDelete });
const theme = useContext(ThemeContext);
return (
<Item key={todo.id} theme={theme}>
<Checkbox
id={todo.id}
label={todo.text}
checked={todo.completed}
onChange={onChange.bind(this, todo.id)}
/>
<Button onClick={onDelete.bind(this, todo.id)} theme={theme}>
x
</Button>
</Item>
);
});
[React] Always useMemo your context value的更多相关文章
- React Hooks: useMemo All In One
React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...
- [React] Validate Compound Component Context Consumers
If someone uses one of our compound components outside the React.createContext <ToggleContext.Pro ...
- [React] Recompose: Theme React Components Live with Context
SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. P ...
- React中useMemo与useCallback的区别
useMemo 把"创建"函数和依赖项数组作为参数传⼊入useMemo,它仅会在某个依赖项改变时才重新计算memoized 值.这种优化有助于避免在每次渲染时都进⾏行行⾼高开销的计 ...
- 手写一个React-Redux,玩转React的Context API
上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...
- React文档(二十二)context
React中,通过React组件可以很容易地追踪数据流.当你关注一个组件,你可以发现哪一个props被传递了,这样使得你的应用很容被推断. 在一些情况下,你想要传递数据通过组件树而不需要去手动在每一层 ...
- 【react】---context的基本使用---【巷子】
一.context的理解 很多优秀的React组件都通过Context来完成自己的功能,比如react-redux的<Provider />,就是通过Context提供一个全局态的stor ...
- React之使用Context跨组件树传递数据
--------------------------------- 讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...
- React 中 context 的使用
官方文档说明(英) 看了别人写的中文博客,再看了官方英文文档,发现还是官方文档讲的浅显易懂一些,看了之后,半翻译半理解地写了这篇博客,更易于新手理解. 介绍 context 是在 react @ 0. ...
随机推荐
- [转帖]prometheus数据采集exporter全家桶
prometheus数据采集exporter全家桶 Rainbowhhy1人评论2731人阅读2019-04-06 15:38:32 https://blog.51cto.com/13053917/2 ...
- 代码中 方法 处提示:This method has a constructor name
“此方法具有构造方法的名字” package classpackage; public class Puppy { public void Puppy(String name) { System.ou ...
- # [洛谷1337] 吊打XXX/平衡点 (模拟退火)
[洛谷1337] 吊打XXX/平衡点 (模拟退火) 题意 n个重物(x,y,w),求平衡时x的位置(x,y) 分析 模拟退火基础题,基于随机数的优化算法,时间复杂度玄学,参数玄学,能不能AC看脸,当然 ...
- X86逆向11:F12暂停法的妙用
本节课将介绍F12暂停法的使用技巧,F12暂停法的原理其实很简单,当我们点击OD中的暂停按钮时,OD会将当前的堆栈状态保存起来,并暂停当前窗体的线程执行,直到我们点击运行按钮OD才会唤醒全部线程并继续 ...
- O051、Create Volume 操作 (Part II)
参考https://www.cnblogs.com/CloudMan6/p/5612147.html 1.cinder-scheduler 也会启动一个工作流 volume_create_ ...
- ui组件库
基于Vue的Quasar Framework 中文网 http://www.quasarchs.com/ quasarframework/quasar: Quasar Frameworkhttps:/ ...
- 【Git的基本操作三】基本操作命令
基本操作 (1) 状态查看操作 git status 作用:查看工作区.暂存区状态 (2) 添加操作 git add [filename] 作用:将工作区文件的 添加/修改,添加到暂存区 (3) 提交 ...
- Samba Server 的使用者帳號及密碼備份
Samba Server 自從 3.x 後改成使用 tdbsam 的方式來管理使用者的帳號及密碼,原本的帳號密碼都是存放在 /etc/samba 目錄之下,最近要做備份時,一時之間竟然找不到 Samb ...
- Delphi 使用Tabel组件的记录查找
樊伟胜
- 【异常】Could not find artifact com.wm.****:
1 详细异常 [ERROR] Failed to execute goal on project spark-etl: Could not resolve dependencies for proje ...