2016-06-15 9 views
0

Ich habe eine grundlegende ES6 react App und versuche MomentJS zu verwenden, um einige Daten zu manipulieren. Aus irgendeinem Grund halte ich month.add is not a functionDaten manipulieren Momentjs in ReactJS

Zur Zeit der Code immer ich habe, ist dies:

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> 
    ) 
} 

}

ich verschiedene Versionen der Einstellung des Anfangszustands mit Moment().month() usw., aber nichts scheint zu funktionieren versucht haben. Jede Hilfe würde sehr geschätzt werden.

Antwort

2

Wenn Sie .format() tun, verwandeln Sie es in eine Zeichenfolge, es ist kein MomentJS-Objekt mehr.

moment().add(1, 'months') // A moment object 

moment().add(1, 'months').subtract(6, 'days') // Can still manipulate 

moment().add(1, 'months').subtract(6, 'days').format() // A final string, can't call moment funcs on it 

Auch gibt es keine Notwendigkeit, mehrere Objekte zu erstellen, wenn sie alle zur gleichen Zeit mit -

const momentNow = Moment(); 

this.state = { 
    currentDate: momentNow.format(), 
    month: momentNow.format('MMMM'), 
    year: momentNow.format('YYYY') 
} 
+1

Vielen Dank für Ihre Antwort. Wenn ich nur ein Objekt im Zustand erstellen würde, zum Beispiel currentDate, wäre es dann möglich, später this.state.currentDate.format ("MMMM") '? –

+0

Ja, es ist möglich. Sie würden nur den HTML-Code aktualisieren. –

+0

Ja, Sie können, obwohl die aktuelle Zeit wahrscheinlich zum Zeitpunkt der Erstellung des Objekts genommen wird, anstatt, wenn Sie ein .format darauf tun - wahrscheinlich kein Problem, aber es lohnt sich, daran zu denken –

1

Ihre state.month eine Zeichenfolge ist. Das verursacht das Problem.

testen

constructor() { 
    super(); 

    this.state = { 
     currentDate: Moment(), 
     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 date = this.state.currentDate; 
    date.add(-1, 'months'); 
    this.setState({ 
     currentDate: date, 
     month: date.format('MMMM'), 
     year: date.format('YYYY') 
    }); 
} 

nextMonth() { 
    let date = this.state.currentDate; 
    date.add(1, 'months'); 
    this.setState({ 
     currentDate: date, 
     month: date.format('MMMM'), 
     year: date.format('YYYY') 
    }); 
}