Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix react lifecycle warning #650

Merged
merged 1 commit into from Dec 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 19 additions & 22 deletions 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) {
Expand All @@ -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();
Expand Down Expand Up @@ -107,7 +100,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 +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;