[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 components. This is done by defining a ref.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>React Lesson 5: Using Refs to Access Components</title>
</head>
<body> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script type="text/jsx"> var React_app = React.createClass({
getInitialState: function() {
return {
red: 128,
green: 128,
blue: 128
}
},
myUpdate: function(){
this.setState({
red: this.refs.red.getDOMNode().value,
green: this.refs.green.getDOMNode().value,
blue: this.refs.blue.getDOMNode().value
});
},
render: function() {
return (
<div>
<Silder update={this.myUpdate} ref="red"></Silder><label>{this.state.red}</label><br/>
<Silder update={this.myUpdate} ref="green"></Silder><label>{this.state.green}</label><br/>
<Silder update={this.myUpdate} ref="blue"></Silder><label>{this.state.blue}</label><br/>
</div>
);
}
}); var Silder = React.createClass({
render: function(){
return (
<input type="range" min="0" max="255" onChange={this.props.update}/>
)
}
}); React.render(<React_app />, document.body);
</script>
</body>
</html>
Here we use getDOMNode() to get the html node:
<Silder update={this.myUpdate} ref="red"></Silder>
then get value from it:
this.refs.red.getDOMNode().value
But, if we add a div:
var Silder = React.createClass({
render: function(){
return (
<div> <!-- added -->
<input type="range" min="0" max="255" onChange={this.props.update}/>
</div> <!-- added -->
)
}
});
We found it doesn't work.
The way can solve this problem is by adding another ref to the input element:
var Silder = React.createClass({
render: function(){
return (
<div >
<input type="range" min="0" max="255" ref="range" onChange={this.props.update}/>
</div>
)
}
});
myUpdate: function(){
this.setState({
red: this.refs.red.refs.range.getDOMNode().value,
green: this.refs.green.refs.range.getDOMNode().value,
blue: this.refs.blue.refs.range.getDOMNode().value
});
},
[React] React Fundamentals: Using Refs to Access Components的更多相关文章
- [React Fundamentals] Using Refs to Access Components
When you are using React components you need to be able to access specific references to individual ...
- [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 组件中使用 Refs 指南
原文:Fullstack React's Guide to using Refs in React Components作者:Yomi Eluwande译者:博轩 译文:https://segment ...
- [React] Use React.cloneElement to Extend Functionality of Children Components
We can utilize React.cloneElement in order to create new components with extended data or functional ...
- 【React】282- 在 React 组件中使用 Refs 指南
英文:Yomi Eluwande 译文:joking_zhang https://segmentfault.com/a/1190000019277029 使用 React 时,我们的默认思维方式应该 ...
- [React] react+redux+router+webpack+antd环境搭建一版
好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- 问题-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 ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
随机推荐
- c++ 学习笔记 c++ 引用C库注意点:#ifdef __cplusplus 倒底是什么意思?
时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #en ...
- asp.net中时间差的问题
asp.net中时间差的问题 在asp中我们可以用datediff来处理,时间的差,相当的不错,可是在asp.net中C#语言中却没有.可是ASP.net给我们提供了一个TimeSpan,我们可以用它 ...
- ANDROID_MARS学习笔记_S02_008_ANIMATION第二种使用方式:xml
一.简介 二.代码1.res\anim下的xml(1)alpha.xml.xml <?xml version="1.0" encoding="utf-8" ...
- Layout Resource官方教程(3)在layout中用include嵌入其它layout
简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...
- c#执行并行任务之Parallel与TaskFactory
任务:几千条(大量)数据往服务器数据库填写.要求单开线程执行,分割成小数据包,多线程运行. 实现方法:Parallel与TaskFactory都可以. 主要代码: Parallel: Barrier ...
- BZOJ3530: [Sdoi2014]数数
3530: [Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 322 Solved: 188[Submit][Status] ...
- NOIP2013 货车运输 LCA倍增+最大生成树
#include <cstdio> #include <iostream> #include <ctime> #include <vector> #in ...
- (转)使用pth文件,让python方便的import自己写的模块
转自:http://www.elias.cn/Python/PythonPath 文章简单易懂,有理有据. 有时候我们正在修改或调试的程序会是一个库,为修改方便,我们可能不大希望把它放到 site-p ...
- css网站导航-菜单
一个简单的网站导航效果: 效果案例:查看演示 css: ;;;} body{font-family: arial, 宋体, serif;font-size: 12px;} .menu{width:11 ...
- vijosP1223麦森数
vijosP1223麦森数 链接:https://vijos.org/p/1223 [思路] 快速幂+高精乘. 计算2^p-1可以快速幂的方法在O(logn)的时间内出解,限于数据范围我们需要用到高精 ...