我使用的是React-native,我有一個我無法解決的問題。反應本機ListView過濾
我有一個帶有x個元素的列表(ListView),用戶可以在不同的日期過濾這個列表。當用戶選擇日期時,我過濾列表並從onClick渲染中調用this.setState()。
這給了我: 「警告:現有的狀態轉變(如render
或其他部件的構造函數中)時,無法更新渲染方法應該是道具和狀態的純函數,構造副作用的抗。模式,但可以移動到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)
});
}
如果您想獲得幫助,您應該顯示一些代碼 – Andrew
顯示Button的代碼。我想這就像 'onClick = {this.setState()}'? – webdeb
我添加了一個代碼片段 – user2354898