Sometimes users of your component want to have more control over what the internal state is. In this lesson we'll learn how to allow users to control some of our component’s state just like the <input />'s value prop.

Controlled props is the prop that component let user fully control.

import React from 'react';

class Toggle extends React.Component {

    // An empty function
static defaultProps = {
defaultOn: false,
onToggle: () => {
}
}; // default state value
initialState = {on: this.props.defaultOn};
state = this.initialState; isControlled() {
return this.props.on !== undefined;
} // toggle method
toggle = () =>{
if (this.isControlled()) {
this.props.onToggle(!this.props.on)
} else {
this.setState(
({on}) => ({on: !on}),
() => {
this.props.onToggle(this.state.on)
}
);
}
}; reset = () => this.setState(
this.initialState
); render() {
return this.props.render({
on: this.isControlled() ?
this.props.on :
this.state.on,
toggle: this.toggle,
reset: this.reset,
getProps: (props) => {
return {
'aria-expanded': this.state.on,
role: 'button',
...props
};
}
});
}
} export default Toggle;

[React] Make Controlled React Components with Control Props的更多相关文章

  1. [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 ...

  2. react 入坑笔记(三) - Props

    React Props props - 参数. 组件类 React.Component 有个 defaultProps 属性,以 class xxx extend React.Component 形式 ...

  3. React基础篇(2) -- state&props&refs

    内容简介 state props refs 行内样式及动态类名 state 基本介绍 React 把组件看成是一个状态机(State Machines).通过与用户的交互,实现不同状态,然后渲染 UI ...

  4. react.js 组件之间的数据传递props

    /* *属性 * 1.如何传递属性 * 2.属性和状态区别和联系 * * 3.子组件都有一个props属性对象 * * 4.单线数据流(只能从父组件流向子组件,就是在父组件定义一个属性,子组件可以通过 ...

  5. [React] Render Text Only Components in React 16

    In this session we create a comment component to explore how to create components that only render t ...

  6. [react] 细数 React 的原罪

    Props & onChange 的原罪 .「props & onChange 接口规范」它不是一个典型的「程序接口规范」. 当你拿到一个可视组件的 ref,却没有类似 setProp ...

  7. React笔记:React基础(2)

    1. JSX JSX是一种拥有描述UI的JavaScript扩展语法,React使用这种语法描述组件的UI. 1.1 基本语法 JSX可以嵌套多个HTML标签,可以使用大部分符号HTML规范的属性. ...

  8. 【react】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  9. react初探索--react + react-router + ant-design 后台管理系统配置

    首先确认安装了node环境,Node >= 6. 如果对react 及 ant-design 一无所知,建议去阅读下api文档,react 可以在 codePen 在线练习. react Api ...

随机推荐

  1. 快速傅里叶变换(Fast-Fourier Transform,FFT)

    数学定义: (详细参考:https://www.baidu.com/link?url=oYAuG2o-pia_U3DlF5n_MJZyE5YKfaVRUHTTDbM1FwM_kDTjGCxKpw_Pb ...

  2. nf_conntrack: table full, dropping packet. 问题

    查出目前 ip_conntrack 记录最多的前十名 IP: # cat /proc/net/nf_conntrack|awk '{print $8}'|cut -d'=' -f 2|sort |un ...

  3. AJAX和JSON实际应用

    实现功能:登录验证 一.因为我是在SpringMVC框架上写的,首先得添加依赖: <dependencies> <!-- 用来测试的依赖 --> <dependency& ...

  4. 转载git的使用

    版权声明:本文为博主原创文章,未经博主允许不得转载.转载请注明原地址 转载请注明出处!谢谢 1.安装Git:Ctrl + Alt + T使用终端:使用命令 [plain] view plain cop ...

  5. shell 特殊字符

    shell 基础 # 当做注释的比较多 : 命令分隔符,在同一行上写两个或两个以上的命令 :: 是case 代码块的结束符 . 点作为文件名的一部分 隐藏文件 目录名 点是正则表达式中的匹配字符 '' ...

  6. Laravel关联模型中过滤结果为空的结果集(has和with区别)

    首先看代码: $userCoupons = UserCoupons::with(['coupon' => function($query) use($groupId){ return $quer ...

  7. 紫书 例题11-7 UVa 753 (网络流最大流)

    设一个源点, 到所有设备连一条弧, 容量为1, 然后设一个汇点, 所有插座到汇点连弧, 容量为1, 然后 转换器也连一条弧, 容量为1. 最后最大流就是答案.其中注意节点数要开大一些. #includ ...

  8. springboot 错误页面的配置

    springboot的错误页面,只需在templates下新建error文件夹,将404.500等错误页面放进去即可, springboot会自动去里面查找

  9. 启用Database Vault

    步骤1:停止EM.监听.数据库 步骤2:启用Database Vault [oracle@single1 ~]$ cd $ORACLE_HOME/rdbms/lib [oracle@single1 l ...

  10. bzoj1012: [JSOI2008]最大数maxnumber(貌似是道线段树喔)

    1012: [JSOI2008]最大数maxnumber 题目:传送门 题解: 发现自己空了一道水题... 1~210000建线段树,其实就是一道裸题... 单点修改+区间查询...1A~ 代码: # ...