React Native - Force ListView Re-render when data has not changed

后端 未结 5 1135
后悔当初
后悔当初 2020-12-30 08:07

Is it possible to force a ListView to re-render, even if the data in the dataSource has not changed? I have a ListView within a tab bar in my app and I want it to redraw eve

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 08:24

    I know I'm a bit late but I'm sure that this is still relevant. There's an even easier approach to this... The first example provided by Jason is by all means a good solution but I'm thinking of those whom easily gets confused. If so, use this.

    // On component initalization
    componentWillMount() {
        this.UpdateData();
    }
    
    // When the component updates
    componentDidUpdate() {
        if (this.state.data != this.props.data)
            this.UpdateData();
    }
    
    // Function to update the ListView component data
    UpdateData() {
        var source = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 !== r2
        });
    
        this.setState({
            data: this.props.data,
            dataSource: source.cloneWithRows(this.props.data)
        });
    }
    

    I also recommend to implement Redux in your application. That way you could store the new, updated array value in a reducer and then store it locally in a state and later use an if statement in the componentDidUpdate function to determine whether the data is updated or not.

    The React debugger logs no performance based complaints or warnings whatsoever...

提交回复
热议问题