React要點整理

bind的意義

以下組件在construtor中使用bind將onClick與類成員函數(shù)綁定:

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的類成員函數(shù)與類實例并不是自動綁定的,constructor是組件裝載時調(diào)用的第一個函數(shù),此時this指向當(dāng)前的類實例,使用bind就可以把成員函數(shù)綁定到這個實例。

prop和state

React 組件的數(shù)據(jù)分為兩種, prop state ,無論 prop 或者 state 的改變,都可能引
發(fā)組件的重新渲染。prop 是組件的對外接口, state 是組件的內(nèi)部狀態(tài),對外用
prop ,內(nèi)部用 state。
prop state 的區(qū)別:

  • prop 用于定義外部接口, state 用于記錄內(nèi)部狀態(tài);
  • prop 的賦值在外部世界使用組件時, state 的賦值在組件內(nèi)部;
  • 組件不應(yīng)該改變 prop 的值,而 state 存在的目的就是讓組件來改變的。

組件向外傳遞數(shù)據(jù)

props用于將數(shù)據(jù)從父組件傳遞給子組件,子組件將內(nèi)部數(shù)據(jù)向父組件傳遞時也可以使用props。這種情況下,父組件要通過props向子組件傳遞可以調(diào)用的函數(shù),子組件通過調(diào)用這個函數(shù)就能把內(nèi)部數(shù)據(jù)傳回給父組件。

例如:
image.png

父組件需要取到內(nèi)部三個子組件的數(shù)值進行計算,可以這樣做:
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與成員函數(shù)onCounterUpdate綁定,因此在子組件調(diào)用onUpdate函數(shù)時,父組件就會通過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
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,079評論 0 29
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • 深入JSX date:20170412筆記原文其實JSX是React.createElement(componen...
    gaoer1938閱讀 8,104評論 2 35
  • GUIDS 第一章 為什么使用React? React 一個提供了用戶接口的JavaScript庫。 誕生于Fac...
    jplyue閱讀 3,587評論 1 11
  • 自己最近的項目是基于react的,于是讀了一遍react的文檔,做了一些記錄(除了REFERENCE部分還沒開始讀...
    潘逸飛閱讀 3,474評論 1 10