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. bzoj2595

    一开始看是插头dp,后来发现还有一个叫斯坦纳树的东西 什么叫斯坦纳树,就是使给定点连通开销和最小的树(可以包含多余的点) 到这张平面图上,我们不难想到用dp来解决,设f[x,y,S]表示连通集合为S, ...

  2. Java集合类:AbstractCollection源码解析

    一.Collection接口 从<Java集合:整体结构>一文中我们知道所有的List和Set都继承自Collection接口,该接口类提供了集合最基本的方法,虽然List接口和Set等都 ...

  3. [转] Android SDK manager 无法获取更新版本列表

      打开这个网址(LINK)就可以看到adt的详细信息. 或者直接在你的eclipse的Help > Install New Software里面add,地址直接输入 https://dl-ss ...

  4. ajax跨域解决方案(服务端仅限java)

    楼主前端知识菜鸟,高手勿喷,在此记录工作中遇到的问题及解决方案,大神请滤过 方法1.jsonp(js客户端ajax请求参数方式设置) 方法2.服务端接口设置: HttpServletResponse ...

  5. 如何添加网站for Linux(绑定域名)

    [以下配置的路径以阿里云提供的标准环境路径为准,如果您另行安装,请根据实际安装路径配置].   1.cd /alidata/server/httpd/conf/vhosts/ 进入绑定域名所在目录, ...

  6. 设置按钮背景图片(HTML-CSS)

    很多人提交表单时都喜欢用一个图片来作为提交按钮,大多数人可能用JS去操作表单的提交,即当用户点击这个图片时响应一个JS来提交表单.其实还有一种方法,就是直接设置SUBMIT按钮的图片背景.设置它的图片 ...

  7. East Central North America Region 2015

    E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...

  8. MapReduce 支持的部分数据挖掘算法

    MapReduce 支持的部分数据挖掘算法 MapReduce 能够解决的问题有一个共同特点:任务可以被分解为多个子问题,且这些子问题相对独立,彼此之间不会有牵制,待并行处理完这些子问题后,任务便被解 ...

  9. javascript设计模式8

    桥接模式(将抽象与其实现隔离开来,以便二者独立变化) function sendInfo(element){ var id=element.id; ajax("GET"," ...

  10. Java学习笔记(1)

    封装基本原则之一: 将实例变量标记为private,将getters与setters标记为public,用setters来检查参数并判断是否可以执行: 局部变量与实例变量的区别: 你无需初始实例变量, ...