[React] React Fundamentals: Component Lifecycle - Updating

The React component lifecycle will allow you to update your components at runtime. This lesson will explore how to do that.

 

Updating: componentWillReceiveProps

componentWillReceiveProps(object nextProps)

Invoked when a component is receiving new props. This method is not called for the initial render.

Use this as an opportunity to react to a prop transition before render() is called by updating the state using this.setState(). The old props can be accessed via this.props. Callingthis.setState() within this function will not trigger an additional render.

 

Updating: shouldComponentUpdate

boolean shouldComponentUpdate(object nextProps, object nextState)

Invoked before rendering when new props or state are being received. This method is not called for the initial render or when forceUpdate is used.

Use this as an opportunity to return false when you're certain that the transition to the new props and state will not require a component update.

If shouldComponentUpdate returns false, then render() will be completely skipped until the next state change. (In addition, componentWillUpdate and componentDidUpdate will not be called.)

By default, shouldComponentUpdate always returns true to prevent subtle bugs when stateis mutated in place, but if you are careful to always treat state as immutable and to read only from props and state in render() then you can override shouldComponentUpdate with an implementation that compares the old props and state to their replacements.

If performance is a bottleneck, especially with dozens or hundreds of components, useshouldComponentUpdate to speed up your app.

 

Updating: componentDidUpdate

componentDidUpdate(object prevProps, object prevState)

Invoked immediately after the component's updates are flushed to the DOM. This method is not called for the initial render.

Use this as an opportunity to operate on the DOM when the component has been updated.

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Lesson 11: Component Lifecycle: Updating</title>
    <script src="http://fb.me/react-0.8.0.js"></script>
    <script src="http://fb.me/JSXTransformer-0.8.0.js"></script>
    <style>
        body {
            margin: 25px;
        }
    </style>
</head>
<body>
<div id="panel"></div>
<script type="text/jsx">
    /** @jsx React.DOM */
    var APP =
            React.createClass({
                getInitialState:function(){
                    return {increasing:false}
                },
                update:function(){
                    var newVal = this.props.val+1
                    this.setProps({val:newVal})
                },
                componentWillReceiveProps:function(nextProps){
                    //Invoked when a component is receiving new props. This method is not called for the initial render.
                    //So when the counter increase, this method will be called
                    //works for props, not state
                    this.setState({increasing:nextProps.val>this.props.val})
                },
                shouldComponentUpdate: function( nextProps,  nextState){
                    /*Invoked before rendering when new props or state are being received. This method is not called for the initial render or when forceUpdate is used.

                     Use this as an opportunity to return false when you're certain that the transition to the new props and state will not require a component update.*/
                    console.log(nextProps.val);
                    //Only update every 5 times
                    return nextProps.val % 5 ===0;
                },
                render:function(){
                    console.log(this.state.increasing)
                    return  (
                            <button
                                    onClick={this.update}>
                                    {this.props.val}
                            </button>
                    )
                },
                componentDidUpdate: function( prevProps,  prevState){
                    console.log("prevProps ===" + JSON.stringify(prevProps));
                }
            });


    React.renderComponent(
            <APP val={0} />,
            document.getElementById('panel'))

</script>
</body>
</html>

 

https://facebook.github.io/react/docs/component-specs.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值