如何使用反应本机路由器通量中的操作从另一个屏幕调用屏幕中的函数

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

嗨,我的项目中有 2 页。当我从第一页转到第二页(使用 Action)时,我 return 使用命令 Action.pop( ) ,这时候我想在第一页调用函数,怎么办?

第一页:

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

第二页:

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

现在,如何调用BacktoFirstPage

你可以做的是将你想要运行的函数作为参数传递给下一个屏幕,然后运行它然后弹出屏幕。

例子

....
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>
    )
}

componentWillUnmount() {
  this.props.onPop();
}

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