[React] Make Controlled React Components with Control Props
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的更多相关文章
- [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 入坑笔记(三) - Props
React Props props - 参数. 组件类 React.Component 有个 defaultProps 属性,以 class xxx extend React.Component 形式 ...
- React基础篇(2) -- state&props&refs
内容简介 state props refs 行内样式及动态类名 state 基本介绍 React 把组件看成是一个状态机(State Machines).通过与用户的交互,实现不同状态,然后渲染 UI ...
- react.js 组件之间的数据传递props
/* *属性 * 1.如何传递属性 * 2.属性和状态区别和联系 * * 3.子组件都有一个props属性对象 * * 4.单线数据流(只能从父组件流向子组件,就是在父组件定义一个属性,子组件可以通过 ...
- [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 ...
- [react] 细数 React 的原罪
Props & onChange 的原罪 .「props & onChange 接口规范」它不是一个典型的「程序接口规范」. 当你拿到一个可视组件的 ref,却没有类似 setProp ...
- React笔记:React基础(2)
1. JSX JSX是一种拥有描述UI的JavaScript扩展语法,React使用这种语法描述组件的UI. 1.1 基本语法 JSX可以嵌套多个HTML标签,可以使用大部分符号HTML规范的属性. ...
- 【react】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- react初探索--react + react-router + ant-design 后台管理系统配置
首先确认安装了node环境,Node >= 6. 如果对react 及 ant-design 一无所知,建议去阅读下api文档,react 可以在 codePen 在线练习. react Api ...
随机推荐
- ADO.NET之断开数据连接的数据库操作
在ADO.NET对数据库操作时有两种方式一种时与数据库实时连接,第二种时断开连接的操作. 断开连接的操作使用SqlDataAdapter来实现,我们要把数据库中的表数据加载到winform中的data ...
- H5发起微信支付
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 一个渣渣python脚本,用wol控制开机
#!/usr/bin/env python #coding:utf8 import os,time d={} '''f = open('E:\info.txt','r') for ipmac in f ...
- 第三方-FastDFS分布式文件系统
1.什么是FastDFS? FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制, 充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标, ...
- [洛谷P1580]yyy loves Easter_Egg I
题目大意:很多人@一个人,如果那个人忍不住说话了,就轰炸成功,如果那个人没说话或者别的人没有@他或@很多个人,则轰炸失败.(具体见原题) 解题思路:字符串处理,好好用sscanf即可(细节见代码). ...
- Vue异步组件Demo
Vue异步组件Demo 在大型应用中,我们可能需要将应用拆分为多个小模块,按需从服务器下载.为了进一步简化,Vue.js 允许将组件定义为一个工厂函数,异步地解析组件的定义.Vue.js 只在组件需要 ...
- docker下修改mysql配置文件
原文:docker下修改mysql配置文件 版权声明:本文为博主原创文章,转载注明地址:http://blog.csdn.net/wang704987562 https://blog.csdn.net ...
- java用jxl实现导出execl表格
//先将需要导出的数据放到list中 //然后将list中的数据放到execl表中 @RequestMapping(params="exportExecl") public Str ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 2.跟我学solr---在solr admin中加入索引
这一章为大家介绍怎样在solr admin中.通过浏览器向solr加入索引 一.加入xml格式的文档 进入solr admin后,点击Documents.选择Documentation Type为xm ...