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. Java5 并发学习

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过 Executor来启动线程比用Thread的start()更好.在新特征 ...

  2. innodb 悲观锁,乐观锁

    转 http://www.cnblogs.com/chenwenbiao/archive/2012/06/06/2537508.html CREATE TABLE `products` ( `id` ...

  3. 关于高斯消元解决xor问题的总结

    我觉得xor这东西特别神奇,最神奇的就是这个性质了 A xor B xor B=A 这样就根本不用在意重复之类的问题了 关于xor的问题大家可以去膜拜莫队的<高斯消元解XOR方程组>,里面 ...

  4. 使用HQL查询

    HQL是Hibernate Query Language的缩写,语法很想SQL,但是HQL是一种面向对象的查询语言.SQL的操作对象是数据列.表等数据库对象,而HQL操作的是类.实例.属性. HQL查 ...

  5. virtualbox中ubuntu和windows共享文件夹设置

    系统平台:win8.1.virtualbox4.3.8.ubuntu12.041.安装VBoxGuestAdditions_4.3.8.iso增强工具,安装完毕后根据提示重启Ubuntu,具体操作如下 ...

  6. android中常用的弹出提示框

    转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的 ...

  7. OD使用经验【转载】

    文章整理发布:黑客风云  1.我的os是winXP,无法使用trw2000,而softice装了多次均未成功,还蓝屏死机多次.郁闷. 2.友好的gui界面,不像softice.可以边干活边听歌,不像s ...

  8. 同样的JS写法,为啥只有IE9模式正常?

    使用 IE F12 开发者工具,选择不同的“文档模式” 从IE7 - IE9,只有IE9正常! <!DOCTYPE html> <html> <script type=& ...

  9. HW6.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  10. java@ 利用ArrayList实现dijkstra算法以及topological 排序算法(java.util.ArrayList)

    package dataStructure; import java.util.ArrayList; import java.util.LinkedList; import java.util.Que ...