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. 获取QQ所有的表情包,包括emoji,动态gif

    获取QQ所有的表情包,包括emoji,动态gif,代码如下. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xht ...

  2. [转] struts.xml配置详解

    转自:http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html struts.xml是我们在开发中利用率最高的文件,也是Struts ...

  3. Dubbo实例

    1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...

  4. 【转载】LVS+MYCAT+读写分离+MYSQL主备同步部署手册(邢锋)

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  5. session问题

    如果 <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424&q ...

  6. HDU 5313 Bipartite Graph

    题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即 ...

  7. linux内核-双向链表

    linux中的经典宏定义 offsetof 定义:offsetof在linux内核的include/linux/stddef.h中定义. #define offsetof(TYPE, MEMBER) ...

  8. codeforce 606B Testing Robots

    题意:给定一个x*y的矩形,和一个机器人的初始位置(x0,y0).以向下为x轴正方向,向右为y轴正方向.现在要对这个机器人进行多次测试.每次测 试,会在矩形的某个位置有一个矿井.所以一共要进行x*y次 ...

  9. rpc,客户端与NameNode通信的过程

    远程过程:java进程.即一个java进程调用另外一个java进程中对象的方法. 调用方称作客户端(client),被调用方称作服务端(server).rpc的通信在java中表现为客户端去调用服务端 ...

  10. POJ1503: Integer Inquiry(连续多个大整数加法运算)

    #include<iostream> #include<cstring> using namespace std; string sum; ; string tool(stri ...