【发布时间】:2016-06-15 16:31:47
【问题描述】:
我有一个基本的 ES6 react 应用程序,我正在尝试使用 momentJS 来操作一些日期。出于某种原因,我不断收到month.add is not a function
目前我的代码是这样的:
export default class CalendarApp extends React.Component {
constructor() {
super();
this.state = {
currentDate: Moment().format(),
month: Moment().format('MMMM'),
year: Moment().format('YYYY')
}
// Bind Methods to this
this.previousMonth = this.previousMonth.bind(this);
this.nextMonth = this.nextMonth.bind(this);
}
previousMonth() {
let month = this.state.month;
month.add(-1, 'months');
this.setState({
month: month
})
}
nextMonth() {
let month = this.state.month;
month.add(1, 'months');
this.setState({
month: month
})
}
render() {
return (
<div className="calendar">
<div className="calendar-container" style={ hideCalendar }>
<caption>
<button className="previous-month" onClick={ this.previousMonth }>Prev</button>
<button className="next-month" onClick={ this.nextMonth }>Next</button>
<div className="calendar-month">
<span>{ this.state.month } { this.state.year }</span>
</div>
</caption>
</div>
</div>
)
}
}
我尝试过使用Moment().month() 等设置初始状态的各种版本,但似乎没有任何效果。任何帮助将不胜感激。
【问题讨论】:
标签: javascript reactjs state momentjs