Skip to content

Commit

Permalink
fix react lifecycle warning
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Dec 25, 2019
1 parent 0cabf95 commit df46a52
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/month/MonthTable.js
@@ -1,43 +1,35 @@
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,
};
static getDerivedStateFromProps(props) {
if ('value' in props) {
return { value: props.value };
}
return null;
}

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
setAndSelectValue(value) {
if (!('value' in props)) {
this.setState({
value: nextProps.value,
value,
});
}
this.props.onSelect(value);
}

setAndSelectValue(value) {
this.setState({
value,
});
this.props.onSelect(value);
chooseMonth(month) {
const next = this.state.value.clone();
next.month(month);
this.setAndSelectValue(next);
}

months() {
Expand Down Expand Up @@ -107,7 +99,7 @@ class MonthTable extends Component {
<td
role="gridcell"
key={monthData.value}
onClick={disabled ? null : chooseMonth.bind(this, monthData.value)}
onClick={disabled ? null : () => this.chooseMonth(monthData.value)}
title={monthData.title}
className={classnames(classNameMap)}
>
Expand All @@ -130,10 +122,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;

0 comments on commit df46a52

Please sign in to comment.