zoukankan      html  css  js  c++  java
  • [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
                });
            },
  • 相关阅读:
    python3爬虫--反爬虫应对机制
    mongodb与mysql区别(超详细)
    cookie和session运行机制、区别
    flask轻量级框架入门
    python自定义元类metaclass,约束子类
    MongoDB ObjectId类型 序列化问题
    【python 内置模块】 返回一个规定长度的随机字符串
    使用PyMongo有多重,使用MongoClientwith的实例时必须小心 fork()
    MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
    分布式文件系统架构HDFS、FastDFS、Haystack
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4363099.html
Copyright © 2011-2022 走看看