[React] Validate Compound Component Context Consumers
If someone uses one of our compound components outside the React.createContext
<ToggleContext.Provider />
, they will experience a confusing error. We could provide a default value for our context, but in our situation that doesn't make sense. Instead let's build a simple function component which does validation of our contextValue
that comes from the <ToggleContext.Consumer />
. That way we can provide a more helpful error message.
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>
)
state = {on: false}
toggle = () =>
this.setState(
({on}) => ({on: !on}),
() => this.props.onToggle(this.state.on),
)
render() {
return (
<ToggleContext.Provider
value={{on: this.state.on, toggle: this.toggle}}
>
{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] Validate Compound Component Context Consumers的更多相关文章
- [React] Make Compound React Components Flexible
Our current compound component implementation is great, but it's limited in that users cannot render ...
- [React] Write Compound Components
Compound component gives more rendering control to the user. The functionality of the component stay ...
- React.createClass 、React.createElement、Component
react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...
- React 中的 Component、PureComponent、无状态组件 之间的比较
React 中的 Component.PureComponent.无状态组件之间的比较 table th:first-of-type { width: 150px; } 组件类型 说明 React.c ...
- React Native 中 component 生命周期
React Native 中 component 生命周期 转自 csdn 子墨博客 http://blog.csdn.net/ElinaVampire/article/details/518136 ...
- [React] Validate Custom React Component Props with PropTypes
In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...
- [React] Compound Component (React.Children.map & React.cloneElement)
Imaging you are building a Tabs component. If looks like: <Tabs> <TabList> <Tab> o ...
- [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] 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 ...
随机推荐
- vba,excel,网址提取名字与链接url
'宏操作 Sub 复制超级链接() '这里控制读取A列的第1到10行,你根据自已的要求修改一下起始和结束行数 ).Hyperlinks.Count > ).Value = Cells(a, ). ...
- 迅为i.MX6UL核心板ARMCortex-A7单核NXP飞思卡尔工控行业Imx6核心板
iMX6UL核心板小巧精致,尺寸仅38mm*42mm:CPU型号iMX6UL@ 528MHz ARM Cortex-A7架构 :内存:512M DDR :存储:8G EMMC,低功耗,性能强大,性价比 ...
- 解决docker pull镜像速度慢的问题
直接下载Docker镜像时,由于种种原因,经常下载失败,即使连接成功也是很慢,怎么办呢 目前我知道可以提升速度的办法:DaoCloud 提供Docker Hub Mirror服务 用户可以进入Dock ...
- pycharm 编写前端代码一些小技巧
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- 第2节 mapreduce深入学习:8、手机流量汇总求和
第2节 mapreduce深入学习:8.手机流量汇总求和 例子:MapReduce综合练习之上网流量统计. 数据格式参见资料夹 需求一:统计求和 统计每个手机号的上行流量总和,下行流量总和,上行总流量 ...
- Spring Data Redis入门示例:Hash操作(七)
将对象存为Redis中的hash类型,可以有两种方式,将每个对象实例作为一个hash进行存储,则实例的每个属性作为hash的field:同种类型的对象实例存储为一个hash,每个实例分配一个field ...
- python使用zipfile解压文件中文乱码问题
中文在编程中真实后娘养的,各种坑爹,python3下中文乱码这个问题抓破了头皮,头疼.看了alex的文章,才有种恍然大悟的感觉(链接在底部). 一句话,就是转换成unicode,压缩前是什么编码,使用 ...
- 原生j获取元素的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 启动web项目卡在Initializing Spring root WebApplicationContext不动
这几天在和同学一起做一个电教器材管理系统的Web项目,用SVN互通,在此记录下经常遇到的bug. Bug: 启动项目一直卡在Initializing Spring root WebApplicatio ...
- 使用sphinx
SQL 结构化查询语言(是一种标准,所有的关系型数据库Mysql,sqlserver,oracle) sphinx的使用两种方式: 第一种: 使用sphinx的API来操作sphinx (常 ...