react初步学习十三

博客围绕组件性能优化展开,重点介绍了 PureRender。PureRender 要求组件满足纯函数条件,官方曾提供 react-addons-pure-render-mixin 插件,其原理是重写 shouldComponentUpdate 方法作浅比较。还阐述了运用和优化 PureRender 的方法,如设置 props 等。

组件性能优化

纯函数

  1. 给定相同的输入,它总是返回相同的输出;
  2. 过程没有副作用(side effect)
  3. 没有额外的状态依赖

PureRender

PureRender 中的 Pure 指的就是组件满足纯函数的条件,即组件的渲染是被相同的 props 和 state 渲染进而得到相同的结果。

PureRender 本质

官方在早期就为开发者提供了名为 react-addons-pure-render-mixin 的插件。其原理为重新实现了 shouldComponentUpdate 生命周期方法,让当前传入的 props和 state 与之前的作浅比较,如果返回 false ,那么组件就不会执行 render 方法。

运用 PureRender

import React, { Component } from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
class App extends Component {
    constructor(props) {
        super(props);
        this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
    }
    render() {
        return <div className={this.props.className}>foo</div>;
    }
}

优化 PureRender

  • 直接为 props 设置对象或数组
    例:
    给Account组件设置style prop :<Account style={{ color: 'black' }} />
    这样设置 prop,则每次渲染时 style 都是新对象
  • 设置 props 方法并通过事件绑定在元素上
import React, { Component } from 'react';
class MyInput extends Component {
    constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
    }
    handleChange(e) {
        this.props.update(e.target.value);
    }
    render() {
        return <input onChange={this.handleChange} />;
    }
}

不用每次都绑定事件,因此把绑定移到构造器内。如果绑定方法需要传递参数,那么可以考虑通过抽象子组件或改变现有数据结构解决。

  • 设置子组件
import React, { Component } from 'react';
class NameItem extends Component {
    render() {
        return (
            <Item>
                <span>Arcthur</span>
            <Item />
        )
    }
}

翻译简化之后:

<Item
    children={React.createElement('span', {}, 'Arcthur')}
/>

复杂典例:

import React, { Component } from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
class NameItem extends Component {
    constructor(props) {
        super(props);
        this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
    }
    render() {
        return (
            <Item>
                <span>Arcthur</span>
            </Item>
        );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值