[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 ...
随机推荐
- 结构体 typedef struct hash_cell_struct hash_cell_t;
typedef struct hash_cell_struct hash_cell_t; struct hash_cell_struct{ void* node; /*!< hash chain ...
- asp mvc 路由
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Workflo ...
- Session简介
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- VC++6.0环境下调试c语言代码的方法和步骤_附图
1.C语言程序四步开发步骤 (1)编辑.可以用任何一种编辑软件将在纸上编写好的C语言程序输入计算机,并将C语言源程序文件*.c以纯文本文件形式保存在计算机的磁盘上(不能设置字体.字号等). (2)编译 ...
- 【jQuery】jQuery操作<input>的聚焦与全选其内容
实现效果: 源代码: $(function() { $("#exist_code_remind").attr("style","display:non ...
- 如何使用ping和tracert命令测试网站访问速度
在我们平时访问的网站中,有一些网站访问速度非常快,比如百度搜索网站和一些门户网站,有些网站访问很慢,有些网站甚至无法访问.那么我们该如何判断这些网站的访问速度呢?下面我们就使用Windows的ping ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
- 转载crontab例行工作调度
转自:http://blog.sina.com.cn/s/blog_95ee143401017y70.html crontab [-e [UserName]|-l [UserName]|-r [Use ...
- CPU的ADDR2为什么跟SDRAM的0地址线接在一起
出处:http://www.100ask.org/bbs/forum.php?mod=viewthread&tid=11544&fromuid=5490 最近看到坛子里很多初学者对于C ...
- 自编译安装nginx
1. 下载nginx,并解压 http://nginx.org/ 2. 下载health check模块 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 1 ...