From 319d66567c737a6277c63a88ef9b23a2d94fb790 Mon Sep 17 00:00:00 2001 From: Cheetah0723 Date: Wed, 25 Dec 2019 10:36:26 +0800 Subject: [PATCH] fix react lifecycle warning (#650) https://github.com/ant-design/ant-design/issues/9792 --- src/month/MonthTable.js | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/month/MonthTable.js b/src/month/MonthTable.js index 2b5a634..31f53fa 100644 --- a/src/month/MonthTable.js +++ b/src/month/MonthTable.js @@ -1,36 +1,23 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; +import { polyfill } from 'react-lifecycles-compat'; import { getTodayTime, getMonthName } from '../util/index'; const ROW = 4; const COL = 3; -function chooseMonth(month) { - const next = this.state.value.clone(); - next.month(month); - this.setAndSelectValue(next); -} - -function noop() { - -} +function noop() {} class MonthTable extends Component { - constructor(props) { - super(props); - - this.state = { - value: props.value, - }; - } + state = { + }; - componentWillReceiveProps(nextProps) { - if ('value' in nextProps) { - this.setState({ - value: nextProps.value, - }); + static getDerivedStateFromProps(props) { + if ('value' in props) { + return { value: props.value }; } + return null; } setAndSelectValue(value) { @@ -40,6 +27,12 @@ class MonthTable extends Component { this.props.onSelect(value); } + chooseMonth(month) { + const next = this.state.value.clone(); + next.month(month); + this.setAndSelectValue(next); + } + months() { const value = this.state.value; const current = value.clone(); @@ -107,7 +100,7 @@ class MonthTable extends Component { this.chooseMonth(monthData.value)} title={monthData.title} className={classnames(classNameMap)} > @@ -130,10 +123,14 @@ class MonthTable extends Component { MonthTable.defaultProps = { onSelect: noop, }; + MonthTable.propTypes = { onSelect: PropTypes.func, cellRender: PropTypes.func, prefixCls: PropTypes.string, value: PropTypes.object, }; + +polyfill(MonthTable); + export default MonthTable;