[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: 0,
            green: 0,
            blue: 0
        }
    }
    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="0"
                       max="255"
                       onChange={this.props.update}
                />
            </div>
        );
        /*return (
            <input type="range"
                   min="0"
                   max="255"
                   onChange={this.props.update}
            />
        );*/
    }
}
时间: 2024-10-13 10:57:45

[React Fundamentals] Using Refs to Access Components的相关文章

WIN7系统 64位出现 Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).

WIN7系统 64位出现  Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).请安装 Microsoft Data Access Components(MDAC)2.6或更高的版本.怎么解决,已经下载了2.8版本安装了,但是还是不顶用. 2015-12-02 10:51网友采纳 这应该是你安装的系统有精简过系统文件,导致安装一些程序缺乏文件出错.换个系统吧.可到我的系统贴吧下载GHO系统与GHO安装工具,可以在进入现在的系

跨数据存取控件Universal Data Access Components

最近发现MDT推出去的系统的有不同问题,其问题就不说了,主要是策略权限被域继承了.比如我们手动安装的很多东东都是未配置壮态,推的就默认为安全壮态了,今天细找了一下,原来把这个关了就可以了. 跨数据存取控件Universal Data Access Components

问题-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 object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed. 问题原因:是启动程序时访问ACCESS库时没有WIN的组件或是版本过低. 解决方法:查看组

[React Fundamentals] Composable Components

To make more composable React components, you can define common APIs for similar component types. import React from 'react'; import ReactDOM from 'react-dom'; export default class App extends React.Component{ constructor(){ super(); this.state = { re

[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 frameworks. This includes AngularJS and D3. A app with React and D3.js: /** @jsx React.DOM */ var App = React.createClass({ getInitialState: function () { re

[React Fundamentals] Accessing Child Properties

When you're building your React components, you'll probably want to access child properties of the markup. In Angular, it is transcludion: <div> <ng-transculde></ng-transclude> </div> In React, it is: {this.props.children} It means

[React Fundamentals] Component Lifecycle - Mounting Basics

React components have a lifecycle, and you are able to access specific phases of that lifecycle. This lesson will introduce mounting and unmounting of your React components. import React from 'react'; import ReactDOM from 'react-dom'; export default

[React Fundamentals] Owner Ownee Relationship

The owner-ownee relationship is used to designate a parent-child relationship with React components as it differs from the DOM relationship. import React from 'react'; export default class App extends React.Component { constructor(){ super(); //This

[React Fundamentals] State Basics

State is used for properties on a component that will change, versus static properties that are passed in. This lesson will introduce you to taking input within your React components. Unlike props, which are meant to be passed into our component as s