[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 ...
随机推荐
- php与其他一些相关工具的安装步骤分享
现在很少花时间来专门写博客,都是把平时看到用到的东西像随笔一样记录在云笔记上. 在这儿分享一些php相关的技术安装过程: linux下编译安装php:php安装 phpunit安装过程:phpunit ...
- WOJ600——水题
第一次做本校OJ的题,被坑的好惨啊! 题目:600.Minimum Distance 题目大意:给定平面上3个点A.B.C,求平面上的任一顶点P,使得|PA|+2|PB|+3|PC|. 由于刚好在这 ...
- Android网站
http://blog.csdn.net/airsaid/article/details/52902299 android调用传感器的代码 http://blog.csdn.net/huangbiao ...
- TCP的链接过程
** TCP 三次握手 四次挥手** 客户端 服务器端 交互的模式 ---> 应答模式* 应答模式 ---> 客户端发起请求 服务器端回应请求** 客户端先去发起请求 ---> 查看 ...
- enote笔记法的思考(ver0.2)
章节:enote笔记法的思考 enote笔记法,它是一种独特的文本标记方式与呈现方式.这一整套系统的记笔记的方法,它能够帮助我们对文本内容(例如,其中的概念.观点.思想等)更加直观和条理地进行理性 ...
- 51node 1134 最长递增子序列 (数据结构)
题意: 最长递增子序列 思路: 普通的$O(n^2)$的会超时.. 然后在网上找到了另一种不是dp的写法,膜拜一下,自己写了一下解释 来自:https://blog.csdn.net/Adusts/a ...
- MySQL与MyBatis中的查询记录
1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...
- delphi clipboard
判断clipboard里的格式: if CliPBoard.HasFormat(CF_TEXT) then EdIT1.Text := CliPBoard.AsText ...
- java使用ant.jar解压缩文件
ant.jar下载地址http://ant.apache.org/bindownload.cgi 压缩文件代码: import org.apache.tools.ant.Project; import ...
- python 类的装饰器
我们知道,在不改变原有代码的基础上,我们可以使用装饰器为函数添加新的功能.同理,一切皆对象,我们也可以使用装饰器为类添加类属性.what? def deco(obj): obj.x = 1 obj.y ...