[React] Prevent Unnecessary Rerenders of Compound Components using React Context
Due to the way that React Context Providers work, our current implementation re-renders all our compound component consumers with every render of the <Toggle />
which could lead to unnecessary re-renders. Let's fix that by ensuring that the value
prop we pass to the <ToggleContext.Provider />
is only changed when the state changes.
// Flexible Compound Components with context
// This allows you to avoid unecessary rerenders import React from 'react'
import {Switch} from '../switch' const ToggleContext = React.createContext() function ToggleConsumer(props) {
return (
<ToggleContext.Consumer {...props}>
{context => {
if (!context) {
throw new Error(
`Toggle compound components cannot be rendered outside the Toggle component`,
)
}
return props.children(context)
}}
</ToggleContext.Consumer>
)
} class Toggle extends React.Component {
static On = ({children}) => (
<ToggleConsumer>
{({on}) => (on ? children : null)}
</ToggleConsumer>
)
static Off = ({children}) => (
<ToggleConsumer>
{({on}) => (on ? null : children)}
</ToggleConsumer>
)
static Button = props => (
<ToggleConsumer>
{({on, toggle}) => (
<Switch on={on} onClick={toggle} {...props} />
)}
</ToggleConsumer>
)
// The reason we had to move `toggle` above `state` is because
// in our `state` initialization we're _using_ `this.toggle`. So
// if `this.toggle` is not defined before state is initialized, then
// `state.toggle` will be undefined.
toggle = () =>
this.setState(
({on}) => ({on: !on}),
() => this.props.onToggle(this.state.on),
)
state = {on: false, toggle: this.toggle}
render() {
return (
<ToggleContext.Provider value={this.state}>
{this.props.children}
</ToggleContext.Provider>
)
}
} function Usage({
onToggle = (...args) => console.log('onToggle', ...args),
}) {
return (
<Toggle onToggle={onToggle}>
<Toggle.On>The button is on</Toggle.On>
<Toggle.Off>The button is off</Toggle.Off>
<div>
<Toggle.Button />
</div>
</Toggle>
)
}
Usage.title = 'Flexible Compound Components' export {Toggle, Usage as default}
[React] Prevent Unnecessary Rerenders of Compound Components using React Context的更多相关文章
- [Angular] Write Compound Components with Angular’s ContentChild
Allow the user to control the view of the toggle component. Break the toggle component up into multi ...
- [React] Render Basic SVG Components in React
React loves svg just as much as it loves html. In this lesson we cover how simple it is to make SVG ...
- react看这篇就够了(react+webpack+redux+reactRouter+sass)
本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fe ...
- Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native
Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...
- React与ES6(四)ES6如何处理React mixins
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- 玩转 React 【第03期】:邂逅 React 组件
上期回顾 前文我们讲解了 React 模板 JSX,接着我们继续来看看 React 组件又是如何工作的呢? 组件化开发到了今天已经是大家的共识,在 React 中,组件同样也是组成我们整个项目的基本单 ...
- [React] Use the Fragment Short Syntax in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...
- 【React自制全家桶】一、Webstrom+React+Ant Design+echarts搭建react项目
前言 一.React是Facebook推出的一个前端框架,之前被用于著名的社交媒体Instagram中,后来由于取得了不错的反响,于是Facebook决定将其开源.出身名门的React也不负众望,成功 ...
- react 16.8版本新特性以及对react开发的影响
Facebook团队对社区上的MVC框架都不太满意的情况下,开发了一套开源的前端框架react,于2013年发布第一个版本. react最开始倡导函数式编程,使用function以及内部方法React ...
随机推荐
- mysql 常用的语句
1.查出当前的字段,放到一个字段中:GROUP_CONCAT(distinct b.patent_ip) ,如 (select GROUP_CONCAT(distinct b.patent_ip) f ...
- Google Chrome浏览器调试
作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.ht ...
- 《BUG创造队》第五次作业:项目需求分析改进与系统设计
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验九 团队作业5-团队项目需求改进与系统设计 团队名称 BUG创造队 作业学习目标 1.编写完整<软件需求规格说明书 ...
- 原生j获取元素的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 零基础入门学习Python(1)--我和Python的第一次亲密接触
前言 最近在学习Python编程语言,于是乎就在网上找资源.其中小甲鱼<零基础入门学习Python>试听了几节课,感觉还挺不错,里面的视频都是免费下载,小甲鱼讲话也挺幽默风趣的,所以呢,就 ...
- css3新特性学习系列 -- border
css3新特性 border属性(border-radius.border-image.box-shadow)详解 1.border-radius 圆角 支持:IE9+ 用法: border-rad ...
- linux core dump 生成和调试
core dump 某些信号的产生会导致产生core dump,包含了进程终止时的内存镜像.在某些时候这个core文件就非常的有用处,配合gdb或者lldb调试起来非常方便. 更详细的文档参考 Lin ...
- 在Linux中CSV转换成XLSX
在linux中,把csv文件转换成excel表格(xlsx或者xls) $ echo -e 'surname,name,age\nCarlo,Smith,23\nJohn,Doe,46\nJane,D ...
- 第二次:Ubuntu16.04 系统怎么截图
一开始想着写文章不用图,全靠文字描述,可是我错了,技术类文字没有图怎么能说的清楚,于是乎开始找在Ubuntu系统下的截图工具,网络神奇,发现了这个,以下命令可以反复试试: ubuntu 会自带一款截图 ...
- LeetCode(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...