React要点整理

bind的意义

以下组件在construtor中使用bind将onClick与类成员函数绑定:

import React, { Component } from 'react';

export default class ClickCounter extends Component {
    constructor(props) {
        super(props);
        this.onClick = this.onClick.bind(this);
        this.state = {
            count: 0
        };
    }

    onClick() {
        this.setState({
            count: this.state.count + 1
        });
    }

    render() {
        return (
            <div>
                <button onClick={this.onClick}>Click Me</button>
                <div>
                    Click Count : {this.state.count}
                </div>
            </div>
        )
    }
}

原因:
ES6的类成员函数与类实例并不是自动绑定的,constructor是组件装载时调用的第一个函数,此时this指向当前的类实例,使用bind就可以把成员函数绑定到这个实例。

prop和state

React 组件的数据分为两种, prop state ,无论 prop 或者 state 的改变,都可能引
发组件的重新渲染。prop 是组件的对外接口, state 是组件的内部状态,对外用
prop ,内部用 state。
prop state 的区别:

  • prop 用于定义外部接口, state 用于记录内部状态;
  • prop 的赋值在外部世界使用组件时, state 的赋值在组件内部;
  • 组件不应该改变 prop 的值,而 state 存在的目的就是让组件来改变的。

组件向外传递数据

props用于将数据从父组件传递给子组件,子组件将内部数据向父组件传递时也可以使用props。这种情况下,父组件要通过props向子组件传递可以调用的函数,子组件通过调用这个函数就能把内部数据传回给父组件。

例如:
image.png

父组件需要取到内部三个子组件的数值进行计算,可以这样做:
import React, {Component} from 'react';
import Counter from './Counter';

export default class ControlPanel extends Component {
    constructor(props) {
        super(props);

        this.onCounterUpdate = this.onCounterUpdate.bind(this);
        this.state = ({
            sum: 30
        });
    }

    onCounterUpdate(previousValue, newValue) {
        this.setState({
            sum: this.state.sum + (newValue - previousValue)
        });
    }

    render() {
        return (
            <div>
                <Counter caption="First" onUpdate={this.onCounterUpdate} />
                <Counter caption="Second" onUpdate={this.onCounterUpdate}  initValue={10} />
                <Counter caption="Third" onUpdate={this.onCounterUpdate}  initValue={20} />
                <hr />
                <span>Total count: {this.state.sum} </span>
            </div>
        )
    }
}

父组件的onUpdate与成员函数onCounterUpdate绑定,因此在子组件调用onUpdate函数时,父组件就会通过onCounterUpdate取得子组件传递的值。

import React, {Component} from 'react';
import PropTypes from 'prop-types';

export default class Counter extends Component {

    constructor(props) {
        super(props);
        
        this.onAdd = this.onAdd.bind(this);
        this.onDecrease = this.onDecrease.bind(this);
        this.state = {
            count: props.initValue || 0
        };
    }

    onAdd() {
        this.updateValue(true);
    }

    onDecrease() {
        this.updateValue(false);
    }

    updateValue(isAdd) {
        const previousValue = this.state.count;
        const newValue = isAdd ? previousValue + 1 : previousValue - 1 ;
        this.setState({
            count: newValue
        });
        this.props.onUpdate(previousValue, newValue);
    }

    render() {
        return (
            <div>
                <button onClick={this.onAdd}>+</button>
                <button onClick={this.onDecrease}>-</button>
                <span>{this.props.caption} count:{this.state.count}</span>
            </div>
        )
    }
}

Counter.propTypes = {
    caption: PropTypes.string.isRequired,
    initValue: PropTypes.number,
    onUpdate: PropTypes.func
}

Counter.defaultProps = {
    initValue: 0,
    onUpdate: f => f
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,082评论 0 29
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,107评论 2 35
  • GUIDS 第一章 为什么使用React? React 一个提供了用户接口的JavaScript库。 诞生于Fac...
    jplyue阅读 3,595评论 1 11
  • 自己最近的项目是基于react的,于是读了一遍react的文档,做了一些记录(除了REFERENCE部分还没开始读...
    潘逸飞阅读 3,499评论 1 10