我正在開發(fā)一個簡單的應(yīng)用程序,背景顏色應(yīng)該根據(jù)季節(jié)而不同。到目前為止我已經(jīng)寫過:class App extends React.Component { constructor() { super(); this.state = { backgroundColor: 'blue' } } handleSeason = (time) => { const months = [ 'January', 'February' 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ] const month = months[time.getMonth()]; if (month === 'January' || month === 'February' || month === 'December') { this.setState({backgroundColor: 'white'}); } else if (month === 'March' || "April" || 'May') { this.setState({ backgroundColor: 'yellow' }); } else if (month==='June' || month ==='July' ||month ==='August'){ this.setState({ backgroundColor: 'green' }); } else { this.setState({ backgroundColor: 'red' }); } } 在渲染中,我返回以下 div: <div className='app' style={{backgroundColor: this.state.backgroundColor }}>背景保持藍色。我不確定問題出在哪里??刂婆_沒有顯示任何錯誤。任何提示將不勝感激。
根據(jù)react.js中的狀態(tài)更改背景顏色
哆啦的時光機
2023-07-06 10:16:05