Search code examples
react-nativereact-native-router-flux

How to call function in a screen from another screen using Actions in react native router flux


Hi I have 2 page in my Project .When I go from the first page to the second page (using Action), then I return to the First page with the command Action.pop() ,At this time I want to call function in first page, how can I do it?

in first page :

.....
BacktoFirstPage(){
    //my code
}
render(){
    return(
        <TouchableOpacity onPress={()=>Actoin.secondPage()}>
            <Text>go to second page</Text>
        </TouchableOpacity>
    )
.....

in second page:

render(){
    return(
        <TouchableOpacity onPress={()=>Actoin.pop()}>
            <Text>back to first page</Text>
        </TouchableOpacity>
    )
}

now , how to call BacktoFirstPage


Solution

  • What you can do is to pass the function you want to run as a parameter to next screen and then run it before pop the screen.

    Example

    ....
    BacktoFirstPage(){
        //my code
    }
    render(){
        return(
            <TouchableOpacity onPress={()=>Actoin.secondPage({onPop: this.BacktoFirstPage.bind(this)})}>
                <Text>go to second page</Text>
            </TouchableOpacity>
        )
    .....
    

    render(){
        return(
            <TouchableOpacity onPress={()=>{ this.props.onPop(); Actoin.pop()}>
                <Text>back to first page</Text>
            </TouchableOpacity>
        )
    }
    

    OR

    componentWillUnmount() {
      this.props.onPop();
    }
    
    render(){
        return(
            <TouchableOpacity onPress={()=>{ Actoin.pop()}>
                <Text>back to first page</Text>
            </TouchableOpacity>
        )
    }