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. Json.net 同过 JsonConverter 调整导出值(未完成)

    public class TimeSpanConverter : JsonConverter { public override bool CanConvert(Type objectType) { ...

  2. uva753 A Plug for UNIX

    最大流. 流可以对应一种分配方式. 显然最大流就可以表示最多匹配数 #include<cstdio> #include<algorithm> #include<cstri ...

  3. WordPress 3.8.1 /xmlrpc.php拒绝服务漏洞

    漏洞版本: WordPress 3.8.1 漏洞描述: WordPress是一款内容管理系统. WordPress 3.8.1 /xmlrpc.php 文件有ping其他主机的功能,通过这个功能可以请 ...

  4. Java String 的equals, == , hascode的区别

    1.equals 和 == ==在java中是比较引用的,即在内存中的地址.而String的equals()是比较字符串的内容 http://blog.csdn.net/barryhappy/arti ...

  5. DICOM医学图像处理:DIMSE消息发送与接收“大同小异”之DCMTK fo-dicom mDCM

    背景: 从DICOM网络传输一文开始,相继介绍了C-ECHO.C-FIND.C-STORE.C-MOVE等DIMSE-C服务的简单实现,博文中的代码给出的实例都是基于fo-dicom库来实现的,原因只 ...

  6. 29、activity横竖屏切换细节问题

    1 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import andro ...

  7. duilib中ListCtrl控件的实现

    转载请说明出处,谢谢~~ 昨天在编程群里聊天,提到了ListCtrl,然后有网友找我,他需要做一个ListCtrl控件,我看过需求后接下了这个活.今天就把大致的思路和过程记录一下.首先看<任务书 ...

  8. bzoj 1060 [ZJOI2007]时态同步(树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1060 [题意] 求最少的增加量,使得以rt为根的树中由一个结点出发的所有到叶子结点的路 ...

  9. Android字符串相关类 - CharSequence

    Class Overview CharSequence定义为public interface.该接口用于表示一个有序字符的集合,并在其中定义里了处理字符的方法. 已知的常用间接子类有String, S ...

  10. 关于图像读取函数imread()的一点使用经验,注意默认参数的赋值

    读入数字图像到数组,用CNN进行训练,发现关于图像读取的一个问题. 问题描述:读取灰度数字图像,在验证时发现存在错误,从图像到数组中的值不完全一样? main code as follows: int ...