Refresh previous screen on goBack()

前端 未结 11 1835
天命终不由人
天命终不由人 2020-12-13 09:34

I am new to React Native. How can we refresh/reload previous screen when returning to it by calling goBack()?

Lets say we have 3 screens A, B, C:

<
11条回答
  •  没有蜡笔的小新
    2020-12-13 10:13

    Thanks to @Bat. I have spent a lot of hours on finding the answer and finally, I got a basic solution which is working according to my needs. I was quite worried though. Simply make a function like this in your previous activity make sure to bind it.

        changeData(){
        var mydata= salesmanActions.retrieveAllSalesman();
        this.setState({dataListFill: mydata});
        alert('' + mydata.length);
        }
    

    Simple, then in constructor bind this,

        this.changeData= this.changeData.bind(this);
    

    After that, as I am using react native navigation, so I will simply pass this function to the second screen just like the code below:

        onPress={() => this.props.navigation.navigate('Add Salesman', {doChange: 
        this.changeData} )}
    

    So when the new screen registered as "Add Salesman" will be called, a parameter named "doChange" which is assigned a function will also be transfered to other screen. Now, in other screen call this method anywhere, by :

        this.props.route.params.doChange();
    

    It works for me. I hope works for you too, THANKS for the idea @Bat.

提交回复
热议问题