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. 《C#并行编程高级教程》第8章 线程池 笔记

    主要的几个概念(详细最好还是看书,配合插图看)   任务是会被分配到线程上的,而这些线程都在线程池引擎下管理 线程池引擎管理着合适数量的线程池,线程从全局队列获取工作项执行. .NET4 Framew ...

  2. HTML.ActionLink 和Html.Action和 Url.Action 的区别

    1. html.ActionLink生成一个<a href=".."></a>标记..例如:@Html.ActionLink(“链接文本”.“someact ...

  3. MFC中状态栏显示鼠标坐标位置

    原文:MFC中状态栏显示鼠标坐标位置,蝈蝈 1,利用MFC向导创建一个应用工程ewq. 2,打开ResourceView,右击Menu菜单,插入Menu,在空白处双击,Caption中填入Point. ...

  4. java泛型小总结

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...

  5. Git管理命令

     1.创建分支 git branch <分支名> 2.切换分支 git checkout <分支名> 创建并切换到该分支:git checkout -b <分支名> ...

  6. pomelo环境搭建

    在ubuntu上搭建pomelo环境 一 安装node.js 不要直接安装nodejs, 因为ubuntu上默认的nodejs版本是0.6的太老千万不要下tar包自己安装, 问题很多正确方法如下:   ...

  7. Base-Android快速开发框架(五)--网络操作之RequestModel、ResponeModel、CustomAsyncHttpClient

    在正式介绍CustomAsyncHttpClient之前,刚好最近有一个朋友找我帮忙给他们看下一个APP.我先上一段代码截图.一段检测版本更新的接口代码.

  8. activemq api的封装

    今天无聊写段代码..学习一下activemq,简单封装了一下activemq 的topic api.跟jdbc很类似 主要代码: import java.io.Serializable; import ...

  9. bzoj 1037 [ZJOI2008]生日聚会Party(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1037 [题意] 一排n男m女,求满足任意连续段男女人数之差不超过k的数目. [思路] ...

  10. ZOJ3469 Food Delivery 区间DP

    题意:有一家快餐店送外卖,现在同时有n个家庭打进电话订购,送货员得以V-1的速度一家一家的运送,但是每一个家庭都有一个不开心的值,每分钟都会增加一倍,值达到一定程度,该家庭将不会再订购外卖了,现在为了 ...