2017-07-01 85 views
0

我使用的是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) 
    }); 

} 
+1

如果您想獲得幫助,您應該顯示一些代碼 – Andrew

+0

顯示Button的代碼。我想這就像 'onClick = {this.setState()}'? – webdeb

+0

我添加了一個代碼片段 – user2354898

回答

0

你的錯誤是在這裏onChange={this.onChange()}

它應該是onChange={ this.onChange }

要調用的道具功能,因此警告消息。

+0

不,這沒有幫助。我仍然收到相同的錯誤。 – user2354898

+0

如果你需要幫助,你應該發佈更多的代碼。您發佈的代碼段在任何反應環境中都無效。發佈整個組件文件 – parker