[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 ...
随机推荐
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)
Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...
- 存储Hyper-V虚拟机的硬盘空间不足时的处理
存储Hyper-V虚拟机的硬盘空间严重不足时的处理 ==先导出虚拟机到空间足够的硬盘,再在空间足够的分区上导入虚拟机 方法如下: 导出虚拟机: 导出之前,我们先删除不需要的快照. 在Hyper-V ...
- Java XSSF 导出excel 工具类
参数解释: title:导出excel标题.headers 导出到excel显示的列头. columns 对应数据库字段 .list 导出数据1.pox中添加依赖 <dependency> ...
- salt 安装kubernetes集群3节点
[root@linux-node1 k8s]# tree .├── etcd.sls├── files│ ├── cfssl-1.2│ │ ├── cfssl-certinfo_linux ...
- 原创全新打包工具Parcel零配置VueJS开发脚手架
parcel-vue 一个基于Parcel打包工具的 VueJS急速开发脚手架解决方案,强烈建议使用node8.0以上 项目地址: https://github.com/w3c-king/p... 初 ...
- logstash-shipper.conf
input { file { path => '/data/rsyslog/*/*/*.log' start_position => 'beginning' sincedb_path =& ...
- 利用shell脚本添加环境变量
在shell脚本设置了环境变量,如export LIBRARY_PATH=./lib/,执行了此脚本后, 在执行生成的可执行文件,提示错误 error while loading shared lib ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 使用java源代码生成Kettle 4.4
kettle作为ETL工具.其功能日趋完好,已得到广大数据挖掘爱好者的青睐.又由于他是java开源项目.为适应项目需求.有必要研究其源代码,最好可以集成到Java项目中.作为项目执行流程的一个重要环节 ...
- Java IO(二) 之 InputStream
源代码均以JDK1.8作为參考 前言: InputStream实现了两个接口Closeable和AutoCloseable: Closeable:JDK1.5中引入,Closeable接口中仅仅有一个 ...