React Native-goBack()上的React Navigation rerender面板

GIV*_*GIV 6 refresh rerender react-native react-navigation

我在我的应用程序中使用React Navigation。返回时如何刷新上一个面板上的数据?

一个实际的例子是:我目前在面板A中,导航到面板B,在其中更新面板A中显示的数据库中的数据。在更改数据库中的数据之后,我对面板A进行goBack()之后,希望面板A现在已重新呈现并保存数据库中的新数据。

我已经读到可以将自定义刷新函数作为参数传递给子类,然后在返回之前调用它,但是我认为这不是实现我想要的方法的一种好方法,因为我更新了尚未安装的组件并且会抛出一个错误。

Jit*_*oni 5

这就是我取得的成就!

在面板A中

refreshFunction()
{
    //your refresh code should be here
}

 <TouchableOpacity onPress={()=>{
   this.props.navigation.navigate('PanelB', {refreshFunction: this.refreshFunction});
 }}>
    Redirect to Panel B
 </TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)

在面板B中

const refreshFunction = this.props.navigation.state.params.refreshFunction;
if(typeof refreshFunction === 'function')
{
  refreshFunction();                
  //your back function
  this.goBack();
}
Run Code Online (Sandbox Code Playgroud)