React 16.4 setState调用componentWillReceiveProps

时间:2018-05-24 16:24:29

标签: reactjs setstate

我正在尝试将react 15.6项目迁移到16.4,但我注意到setState方法的行为非常奇怪。检查此组件:

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

export default class TestCompo extends Component {

  static propTypes = {};

  constructor (props) {
    super(props);

    this.onClick = ::this.onClick;
  }

  state = {
  };

  componentWillReceiveProps (nextProps, nextContext) {
    console.log('componentWillReceiveProps');
  }

  onClick () {
    this.setState(() => ({
      clickCount: this.state.clickCount + 1
    }));
  }

  render () {
    return (
     <div>
        <button type='button' onClick={this.onClick}>Test buttons</button>
      </div>
    );
  }
}

在React 15.6上,一切正常,但在React 16.4上按下按钮也是componentWillReceiveProps - 尽管没有任何道具被更新。有人知道原因吗?

0 个答案:

没有答案