[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),然而网上搜到的很多教 ...
随机推荐
- mjpg-streamer on raspberrypi
http://sourceforge.net/projects/mjpg-streamer/ svn address svn checkout svn://svn.code.sf.net/p/mjpg ...
- 第十章Composite设备
10.1 Composite设备介绍 USB的Composite类是USB 复合设备类,一个USB设备具有多种设备功能,比如一个USB设备同时具有鼠标和键盘功能.单一的USB设备开发相对简单,但在很多 ...
- hdu4722Good Numbers(dp)
链接 这题规律其实挺明显的 打表找规律估计都可以 正规点就是DP 算出第N位所包含的good number的数量 如果给出的数是N+1位 就枚举各位上比原来小的数 加上下一位的dp值 一个i写成g了 ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- sqliteExpert软件使用(创建数据库和表)
sqliteExpert是sqlite数据库的可视化操作软件,通过该软件可以进行可视化的创建数据库以及表,免去了复杂的建表语句.首先在下面地址下载该软件http://www.ddooo.com/sof ...
- epub-2格式电子书剖析之一:文档构成
epub格式电子书遵循IDPF推出的OCF规范,OCF规范遵循ZIP压缩技术,即epub电子书本身就是一个ZIP文件,我们将epub格式电子书的后缀.epub修改为.zip后,可以通过解压缩软件(例如 ...
- c#中字符串截取使用的方法
AndyZhang welcome to java world c#中字符串截取使用的方法 String substring(int beginIndex) String substring(int ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents
I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry t ...
- mysql服务启动 但端口未监听
mysql 启动了,用 localhost 可以连接,但是用 127.0.0.1 不能连接.可能的原因是 1. mysql为了增强安全性而跳过了端口监听,查看方法: 用mysql> SHOW V ...
- modsecookie
Cookie parsing added添加cookie解析 现在你可以使用新的可选的过滤器变量(COOKIE_name, COOKIE_NAMES, COOKIE_VALUES)分析cookies. ...