React-Native SwipeableListView不正确更新

问题描述:

在SwipeableListView中滑动行时我想删除rowitem并重新呈现列表。React-Native SwipeableListView不正确更新

现在发生的情况是,总是删除列表中的最后一项,而不是被删除的项目。 任何想法是什么错?

export default class SwipeList extends Component { 
    constructor(props) { 
    super(props); 
    let ds = SwipeableListView.getNewDataSource(); 

    this.favourites = [] 

    this.state = { 
    ds:[], 
    dataSource:ds, 

    isLoading:true, 
    closeRow:false, 
    }; 
    } 

componentWillMount() { 
    store.get('KEY_FAV').then(value => { 
    typeof(value) === 'object' 
     ? this.favourites = Object.keys(mockdata.favourite) 
     : this.favourites = JSON.parse(value) 

    this.setState({ 
     dataSource: this.state.dataSource.cloneWithRowsAndSections(this.genData(this.favourites 
)), 
     isLoading:false 
    }) 
    }) 
} 


genData = (list) => { 
    let dataBlob = [] 

    for(let i = 0; i <list.length; i++) { 
    dataBlob.push({id:list[i], name:list[list[i]]}) 
    } 
return [dataBlob, []] 
} 

直到这里没关系,SwipeableList加载了所有的RowItems。 但在下面的handleSwipeAction()为dataSource设置新状态时,列表将只删除最后一个项目,而不是选中的项目。

handleSwipeAction = (rowData, SectionID, rowID) => { 
    AlertIOS.alert('Remove ' + rowData.name + ' \nfrom Favourites?', null, 
    [ 
    {text:'Cancel', onPress:() => {this.setState({closeRow:true})}, style:'cancel'}, 
    {text:'OK', onPress:() => { 
     this.favourites.slice() 
     this.favourites.splice(rowID, 1) 

     this.setState({ 
     closeRow:true, 
     }) 
     this.setState({//I THINK HERE IS THE PROBLEM 
     dataSource:this.state.dataSource.cloneWithRowsAndSections(this.genData(this.favourites)) 
     }) 

     store.set('KEY_FAV', this.favourites)     
    }} 
    ]) 
} 

onSwipe = (rowData, SectionID, rowID) => { 
    return (
    <View style={styles.actionsContainer}> 
     <TouchableHighlight 
     onPress={() => this.handleSwipeAction(rowData, SectionID, rowID)}> 
     <Text style={styles.actionsItem}>Remove</Text> 
     </TouchableHighlight> 
    </View> 
    ); 
    }; 

和渲染功能

render() { 
if(this.state.isLoading) return null 

return (
    <View style={styles.container}> 

     <SwipeableListView 
     bounceFirstRowOnMount 
     enableEmptySections={true} 
     dataSource={this.state.dataSource} 
     maxSwipeDistance={this.props.swipeDistance} 
     renderRow={(item) => this.renderItem(item)} 
     renderQuickActions={this.onSwipe} 
     renderSeparator={this.renderSeperator} 
     doCloseRow={this.state.closeRow} 
     /> 
    </View> 
); 

}

当您完成slicing,我相信,如果你这样做:

let ds = SwipeableListView.getNewDataSource();一遍,然后

this.setState({ dataSource: ds.cloneWithRowsAndSections(this.genData(this.favourites)) }) 

它应该工作。由于我仍然没有得到的原因。我也不知道你为什么在你的功能中做两个setState()。一个是不够的?

所以这应该工作:

handleSwipeAction = (rowData, SectionID, rowID) => { 
    AlertIOS.alert('Remove ' + rowData.name + ' \nfrom Favourites?', null, 
    [ 
    {text:'Cancel', onPress:() => {this.setState({closeRow:true})}, style:'cancel'}, 
    {text:'OK', onPress:() => { 
     this.favourites.slice() 
     this.favourites.splice(rowID, 1) 

     let ds = SwipeableListView.getNewDataSource(); // add this 

     this.setState({ dataSource: ds.cloneWithRowsAndSections(this.genData(this.favourites)), closeRow:true }) 

     store.set('KEY_FAV', this.favourites) }} ]) 
} 
+0

谢谢,但是这也不能正常工作,该名单现在一样的原始列表。没有项目被删除?如果我将它们放在一个语句中,两个setState()的原因是/是,它们将不起作用。 –

+0

我以前有数据源不更新的问题,我的答案是解决方案。现在,如果您确实声明了一个新的数据源,并用新数据克隆了这些数据,但没有看到任何更改,这意味着数据不是新的或者没有更改

+0

以上解决方案是正确的。我的代码的实际问题不是'setState({dataSource:..)}',而是'setState({closeRow:true)}',我改变了的代码以便能够关闭作为道具的一排。 'if(this.state.closeRow!== nextProps.closeRow){this.state.dataSource.setOpenRowID(null),}) }。这阻止了SwipeAbleListView的呈现。 –