我正在使用React-native,我遇到了一个我无法解决的问题 .
我有一个带有x元素的列表(ListView),用户可以在不同的日期过滤此列表 . 当用户选择日期时,我过滤列表并从onClick in render中调用this.setState() .
这给了我:“警告:在现有状态转换期间无法更新(例如在 render
或其他组件的构造函数中) . 渲染方法应该是props和state的纯函数;构造函数的副作用是反模式,但可以是搬到 componentWillMount
. “
我得到这个是因为我在渲染中有一个调用setState的按钮 .
我尝试使用componentWillMount,componentWillUpdate,componentDidUpdate但它不起作用 .
<Accordion
sections={this.state.uniqueAvailableTimeSlots}
renderHeader={this.renderAccordionHeader}
renderContent={this.renderAccordionBody}
onChange={this.onChange()}
/>
//OnChange will be called when a filter is choosen
//I filter a list and put it in filteredTimeSlots
onChange = () => {
const filteredTimeSlots = this.state.availableTimeSlots.filter((timeSlot) => {
return timeSlot.timeSlot.includes(this.state.uniqueAvailableTimeSlots[index]);
});
//I set the state for the new list
this.setState({
availableTimeSlotsFiltered: this.state.availableTimeSlotsFiltered.cloneWithRows(filteredTimeSlots)
});
}
1 回答
你的错误在这里
onChange={this.onChange()}
它应该是
onChange={ this.onChange }
您正在调用props中的函数,因此警告消息 .