[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. ...
随机推荐
- 【LOJ】#3083. 「GXOI / GZOI2019」与或和
LOJ#3083. 「GXOI / GZOI2019」与或和 显然是先拆位,AND的答案是所有数字为1的子矩阵的个数 OR是所有的子矩阵个数减去所有数字为0的子矩阵的个数 子矩阵怎么求可以记录每个位置 ...
- spring事务使用
spring的事务管理有几种方式实现,如何实现? 事务的实现方式:实现方式共有两种:编码方式:声明式事务管理方式 基于AOP技术实现的声明式事务管理,实质就是:在方法执行前后进行拦截,然后再目标方法开 ...
- Lucky Sorting(CodeForces-109D)【思维】
题意:给出一组数,要求从小到大排序,并且排序的过程中,发生交换的两个数至少一个为幸运数(十进制位均为4或7),问能否在(2×n)次交换内完成排序,如果能,输出交换的方案(不要求步骤数最少). 思路:首 ...
- 【原创】大叔经验分享(73)scala akka actor
import java.util.concurrent.{ExecutorService, Executors, TimeUnit} import akka.actor.{Actor, ActorSy ...
- luogu题解P4198楼房重建--线段树神操作
题目链接 https://www.luogu.org/problemnew/show/P4198 分析 一句话题意,一条数轴上有若干楼房,坐标为\(xi\)的楼房有高度\(hi\),那么它的斜率为\( ...
- gcc/g++ 以及makefile
生成可执行文件 g++ mutiprocess.cpp -o test -fpic:产生位置无关码,位置无关码就是可以在进程的任意内存位置执行的目标码,动态链接库必须使用 -c : 只生成 .o ...
- Selenium 基本使用
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- Kendall tau距离(即两个内容相同的数组中逆序数对的数量)(算法》P220 第2.5.3.2小节)
一组排列就是一组N个整数的数组,其中0~N-1的每个数都只出现一次.两个排列之间的 Kendall tau距离就是在两组排列中相对顺序不同的数对的数目.例如,0 3 1 6 2 5 4和1 0 3 6 ...
- vue页面中图片不显示解决
在做新版组态界面的时候,用vue框架实现,在配置页面图片的时候发现有一张图片明明页面输入的路径是对的可是图片就是不显示出来 现象: network页面资源也不报错,而且状态码竟然还是200,点prev ...
- 【vue+axios】一个项目学会前端实现登录拦截
原文链接:github.com 一个项目学会vue全家桶+axios实现登录.拦截.登出功能,以及利用axios的http拦截器拦截请求和响应. 前言 该项目是利用了Github 提供的persona ...