When you are using React components you need to be able to access specific references to individual components. This is done by defining a ref.

Notice: 'ref' only works in class component, not in statless component.

If we don't add "ref", three slider will mutate the same state, they won't have isolated scope.

If we want they behave differently, then we need to use it.

import React from 'react';
import ReactDOM from 'react-dom'; export default class App extends React.Component {
constructor(){
super();
this.state = {
red: ,
green: ,
blue:
}
}
update(){
this.setState({
red: ReactDOM.findDOMNode(this.refs.red.refs.inp).value,
green: ReactDOM.findDOMNode(this.refs.green.refs.inp).value,
blue: ReactDOM.findDOMNode(this.refs.blue.refs.inp).value
})
} render() {
return (
<div>
<Slider ref="red" update={this.update.bind(this)}></Slider>
{this.state.red}
<hr />
<Slider ref="green" update={this.update.bind(this)}></Slider>
{this.state.green}
<hr />
<Slider ref="blue" update={this.update.bind(this)}></Slider>
{this.state.blue}
<hr />
</div>
)
}
} class Slider extends React.Component{
render(){
//refs will point to the input tag
//if you wrap input inside div
//then you need to add another ref to the input
//and access input like "this.refs.red.refs.inp"
return (
<div>
<input type="range" ref="inp"
min=""
max=""
onChange={this.props.update}
/>
</div>
);
/*return (
<input type="range"
min="0"
max="255"
onChange={this.props.update}
/>
);*/
}
}

[React Fundamentals] Using Refs to Access Components的更多相关文章

  1. [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 ...

  2. 问题-Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed.

    问题现象:软件在启动时报如下错误信息:Exception Exception in module zhujiangguanjia.exe at 001da37f. Error creating obj ...

  3. Windows 的 Oracle Data Access Components (ODAC)

     下载 x64bit https://www.oracle.com/technetwork/cn/database/windows/downloads/index.html 适用于 Windows 的 ...

  4. WIN7系统 64位出现 Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).

    WIN7系统 64位出现  Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).请安装 Microsoft Data Acces ...

  5. [React Fundamentals] Composable Components

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

  6. [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 ...

  7. [React Fundamentals] Component Lifecycle - Mounting Basics

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

  8. [React Fundamentals] Accessing Child Properties

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

  9. [React] React Fundamentals: Component Lifecycle - Mounting Basics

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

随机推荐

  1. poj 2253 Frogger(最短路 floyd)

    题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...

  2. gdb调试SAPI方式的php

    一.修改php-fpm.conf文件 /usr/local/php/etc/php-fpm.conf pm.max_children = 1 #只产生一个进程,便于追踪 二.得到进行服务的进程号 [r ...

  3. echarts 问题2

    加载图表的区域div必须单独给定class,此样式中必须还有宽和高,不然图表出来之后会有这样那样的问题

  4. Oracle tnsname.ora 链接问题

    oracle数据库需要配置tns链接 这里我发现了一个问题: 在D:\Oracle\product\10.1.0\Client_3\NETWORK\ADMIN 目录中配置链接字符串的时候要特别注意: ...

  5. bzoj1061: [Noi2008]志愿者招募

    线性规划与费用流.http://www.cnblogs.com/iiyiyi/p/5616080.html.数组范围开错了!!!然后2.31-1=0x7fffffff!=0x7f7f7f7f. 开始以 ...

  6. 记录一次Jmeter性能测试

    一.引言 之前有总结过如何写Java请求测试用例类,写完测试脚本调通之后,信心满满地以为我准备好可以开始性能测试了.结果在评审测试计划的时候,当即被项目组狠狠的扇了一耳光,各种不确定的点:性能指标不明 ...

  7. bug描述注意点

    一个好的错误跟踪系统包括了错误的必要信息,如果做得不好,会造成迷惑,并误导读者.好的故障描述应该包括十个基本部分:标题.项目.所属模块.优先级.重要性.异常等级.可重复性.现象.操作过程和附件. ①标 ...

  8. 《GettingThingsDone》--GTD学习笔记(三)-GTD的三个关键原则

    原则一:养成收集的习惯 1.收集习惯给个人带来的好处     在收集过程中你会出现焦虑和解脱,难以招架和控制良好的情绪. (1)消极情绪的来源     要做的事情总比你能做的事情多,要做的事情太多并不 ...

  9. CA1060

    Move P/Invokes to NativeMethods class 规则描述: 平台调用服务访问非托管代码. 平台调用方法(使用了System.Runtime.InteropServices. ...

  10. Serach

    1.二分查找 public class BubbleSort { public static int binarySerach(int[] a,int value){ int low=0; int h ...