javascript - react 导航 goBack() 并在上一个屏幕中更新

标签 javascript react-native react-navigation

我有两个屏幕。每个屏幕包含三个子类。是的,我正在使用每个屏幕的选项卡。 在我的第一个屏幕上说。 主屏幕 有 -> ScreenA、ScreenB、ScreenC。我的 DetailScreen 有 -> ScreenD、ScreenE、ScreenF

现在,从我的 ScreenA 中,我必须按下去 ScreenD。代码在这里:

  this.props.navigation.navigate('DetailScreen', {
            onNavigateBack: this.refresh.bind(this)
   })

 refresh(commentText) {
        alert('alert me')
    }

在我的 ScreenD 中,我有一个返回按钮。并更新我的 ScreenA 中的值:

 this.props.navigation.state.params.onNavigateBack(this.state.commentText); 
 this.props.navigation.goBack();

但我总是会收到这样的错误:

Undefined is not an object (evaluating 'this.props.navigation.state')

任何帮助都会很棒。我的目标是在我从 screen D

回来时更新我的​​ screen A

我的主屏幕:

 <ScreenA navigation={this.props.navigation}  tabLabel= "ScreenA" props = {this.props} tabView={this.tabView}/>
 <ScreenB navigation={this.props.navigation}  tabLabel= "ScreenB" props = {this.props} tabView={this.tabView}/>
<ScreenC navigation={this.props.navigation}   tabLabel= "ScreenC" props = {this.props} tabView={this.tabView} goToPage={ () => this.goToPage(2) }/>

DetailScreen:

 <ScreenD navigation={this.props.navigation}  tabLabel= "ScreenD" props = {this.props} tabView={this.tabView}/>
 <ScreenE navigation={this.props.navigation}  tabLabel= "ScreenE" props = {this.props} tabView={this.tabView}/>
<ScreenF navigation={this.props.navigation}   tabLabel= "ScreenF” props = {this.props} tabView={this.tabView} goToPage={ () => this.goToPage(2) }/>

我的 RootStackScreen:

import React from 'react';

import { createStackNavigator } from '@react-navigation/stack';

import ScreenA from './ScreenA';
import ScreenB from './ScreenB';

const RootStack = createStackNavigator();


const RootStackScreen = ({navigation}) => (
    <RootStack.Navigator headerMode='none'>
        <RootStack.Screen name="ScreenA" component={ScreenA}/>
        <RootStack.Screen name="ScreenB" component={ScreenB}/>
    </RootStack.Navigator>
);


export default RootStackScreen;

最佳答案

1) 转到项目中的 package.json 文件。

2) 将@react-navigation/stack 的版本更改为5.x.x

例子:

"@react-navigation/stack": "^5.2.14",

3) 运行 npm install

4) 在你的 ScreenA 中,

this.props.navigation.navigate('DetailScreen', {
  onNavigateBack: (commentText) => this.refresh(commentText)
})

refresh(commentText) {
  alert('alert me')
}

5) 在你的 ScreenD 中,

this.props.route.params.onNavigateBack(this.state.commentText); 
this.props.navigation.goBack();

6) 运行 npm start 并重新启动您的项目。

您将解决问题。

请确保您使用的是 @react-navigation/stack: ^5.x.x

这是一个工作示例...

https://snack.expo.io/K1Kb_iTEW

关于javascript - react 导航 goBack() 并在上一个屏幕中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62403902/

相关文章:

ios - 在 React-Native 中将 RCTRootView 作为 subview 添加到 rootViewController

javascript - 每当我使用 props.navigation.navigate ('Game' 时,在 HeaderBackButton.tsx 中 react native 导航问题)

android - React Native Expo Android WebView

javascript - ajax Miltipart FormData - 在一个请求中上传数据和文件

javascript - 用JS将php变量拆分为数组

javascript - 带有 SQL 查询问题的准备语句

react-native - 无法使用 react-native-paper 和 @react-navigation/bottom-tabs 滚动

reactjs - 在 React Native 中使用 React Navigation 隐藏 StackNavigator 上的 header

react-native - 路由 'Main'的组件必须是React组件

javascript - 如何正确实现带id的路由?