To make more composable React components, you can define common APIs for similar component types.

import React from 'react';
import ReactDOM from 'react-dom'; export default class App extends React.Component{
constructor(){
super();
this.state = {
red: 0,
green: 0,
}
}
update(){
this.setState({
red: ReactDOM.findDOMNode(this.refs.red.refs.inp).value,
green: ReactDOM.findDOMNode(this.refs.green.refs.inp).value,
})
}
render(){
return(
<div>
<NumInput
ref="red"
type="range"
min={0}
max={255}
step={1}
val={+this.state.red}
label="Red"
update={this.update.bind(this)}
></NumInput>
<NumInput
ref="green"
type="number"
step={0.01}
val={+this.state.green}
label="Green"
update={this.update.bind(this)}
></NumInput>
</div>
);
}
} class NumInput extends React.Component{
constructor(){
super();
}
render() {
const label = this.props.label ?
<label>{this.props.label} - {this.props.val}</label> :
'';
return (
<div>
<input
type={this.props.type}
min={this.props.min}
max={this.props.max}
step={this.props.step}
defaultValue={this.props.val}
onChange={this.props.update}
ref="inp"
/>
{label}
</div>
);
}
} NumInput.propTypes = {
type: React.PropTypes.oneOf(['range', 'number']),
min: React.PropTypes.number,
max: React.PropTypes.number,
step: React.PropTypes.number,
val: React.PropTypes.number,
label: React.PropTypes.string,
update: React.PropTypes.func.isRequired,
}; NumInput.defaultProps = {
type: 'range',
min: 0,
max: 255,
step: 1,
val: 0,
label: ''
};

[React Fundamentals] Composable Components的更多相关文章

  1. [React] React Fundamentals: Integrating Components with D3 and AngularJS

    Since React is only interested in the V (view) of MVC, it plays well with other toolkits and framewo ...

  2. 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

    原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...

  3. Facebook React 和 Web Components(Polymer)对比优势和劣势

    目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...

  4. [React Fundamentals] Using Refs to Access Components

    When you are using React components you need to be able to access specific references to individual ...

  5. [React] React Fundamentals: Using Refs to Access Components

    When you are using React components you need to be able to access specific references to individual ...

  6. [React] 09 - Tutorial: components

    jsx变为js的过程:http://babeljs.io/repl/ youtube: https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg ...

  7. [React Fundamentals] Component Lifecycle - Updating

    The React component lifecycle will allow you to update your components at runtime. This lesson will ...

  8. [React Fundamentals] Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  9. [React Fundamentals] Accessing Child Properties

    When you're building your React components, you'll probably want to access child properties of the m ...

随机推荐

  1. Entityframework常用命令

    Enable-Migrations 启用Migration数据迁移 Add-Migration migrationname 添加一个migration Update-Database –TargetM ...

  2. mysql qps tps

    (1)QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...

  3. 设计模式:Observer(观察者)—— Guava EventBus

    本文分为三个部分:   Observer(观察者) Guava EventBus详解 Guava EventBus使用示例   1. Observer(观察者)   1.1 背景   我们设计系统时, ...

  4. (Android Studio)添加按钮以及权重问题

    此文大部分摘自http://hukai.me/android-training-course-in-chinese/basics/firstapp/building-ui.html 注意到光标所在代码 ...

  5. Kettle简介

    ETL和Kettle简介     ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...

  6. uva11992-Fast Matrix Operations(区间增值、改值)

    题意: r行c列的全0矩阵   有三种操作 1 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素增加v 2 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素设为v ...

  7. error: Setup script exited with error: Unable to find vcvarsall.bat

    安装mxnet python版本时遇到“Unable to find vcvarsall.bat”错误搜索一下后查到如下方法: python 3.5.2版本依赖高版本的vs解决办法是安装vs2015的 ...

  8. cocos2d-x 2.x版本中,场景切换各方法调用顺序

    假设从A场景切换到B场景,调用各场景方法的顺序为: 如果没有切换效果(transition),则先调用B的init(),再调用A的onExitTransitionStart(),接着调用A的onExi ...

  9. [NOI导刊2011]影像之结构化特征

    问题描述 在影像比对中,有一种方法是利用影像中的边缘(edge)资讯,计算每个边缘资讯中具有代表性的结构化特征,以作为比对两张影像是否相似的判断标准.Water-filling方法是从每个边缘图的一个 ...

  10. 29个你必须知道的Linux命令

    虽然Linux发行版支持各种各样的饿GUI(graphical user interfaces),但在某些情况下,Linux的命令行接口(bash)仍然是简单快速的.Bash和 Linux Shell ...