[React Fundamentals] Using Refs to Access Components
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的更多相关文章
- [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 ...
- 问题-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 ...
- Windows 的 Oracle Data Access Components (ODAC)
下载 x64bit https://www.oracle.com/technetwork/cn/database/windows/downloads/index.html 适用于 Windows 的 ...
- WIN7系统 64位出现 Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).
WIN7系统 64位出现 Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).请安装 Microsoft Data Acces ...
- [React Fundamentals] Composable Components
To make more composable React components, you can define common APIs for similar component types. im ...
- [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 ...
- [React Fundamentals] Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
- [React Fundamentals] Accessing Child Properties
When you're building your React components, you'll probably want to access child properties of the m ...
- [React] React Fundamentals: Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
随机推荐
- bzoj1084: [SCOI2005]最大子矩阵
dp.状态转移方程在代码里 #include<cstdio> #include<algorithm> #include<cstring> using namespa ...
- 浏览器兼容问题系列---使IE支持CSS3 Media Quary
兼容是一件很让前端攻城师头疼的事情,笔者今天在做一个Demo的时候就碰到了一个问题(大牛就不要拍砖了,谢谢!) 经常做移动互联网前端的攻城师想必对于css3 media quary已经很熟悉了,但是碰 ...
- BZOJ3232: 圈地游戏
题解: 神题一道... 题解戳这里:http://hi.baidu.com/strongoier/item/0425f0e5814e010265db0095 分数规划可以看这里:http://blog ...
- mysql 触发器学习(可以将mysql数据同步到redis)
1. 一个简单的例子 1.1. 创建表: create table t(s1 integer); 1.2. 触发器: delimiter | create trigger t_trigger befo ...
- Xcode8安装不成功, 需要升级系统. The operation couldn't be completed. cpio read error
https://developer.apple.com/library/prerelease/content/documentation/DeveloperTools/Conceptual/Whats ...
- ORACLE DATAGURARD配置手记
经过多次实践,参阅网上N多文章……最后还是配不成,可能本人悟性太低,无法体会高手的笔记.最终还是在前辈的帮助下完成.特用最平实的手法记录下来,以便如吾辈菜鸟能 看得懂. 运行Data Guard的条件 ...
- JQuery: 微博演示
演示图: 完成图: 微博演示代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> ...
- java设计模式—Adapter模式
1.核心意图: 将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作. 该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adap ...
- jetty属性
jetty 版本信息 Jetty7 - 此插件更名为jetty-maven-plugin,以便更符合maven2的协定.为了在Web应用做快速应用开发做准备,详见多Web应用源目录. 为 ...
- 设计模式_Composite_合成模式
形象例子: Mary今天过生日.“我过生日,你要送我一件礼物.”“嗯,好吧,去 商店,你自己挑.”“这件T恤挺漂亮,买,这条裙子好看,买,这个包也不错,买.”“喂,买了三件了呀,我只答应送一件礼物的哦 ...