【问题标题】:React navigation goBack() and update in previous screen反应导航 goBack() 并在上一个屏幕中更新
【发布时间】:2020-06-16 08:10:48
【问题描述】:

我有两个屏幕。每个屏幕包含三个子类。是的,我正在使用每个屏幕的选项卡。 在我的第一个屏幕上说。 Main-screen 有 -> 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;

【问题讨论】:

  • 您能否添加您的 ScreenA 或 ScreenB 之一的代码?
  • @Sennen Randika 我正在使用平面列表。没有其他的。它非常大,这就是为什么不在这里发布。 export default class MainScreen extends Component {}
  • 嗨@kumarbarian,你使用哪个版本的react-navigation?如果你只有this.props.navigation.goBack();,它会返回上一个屏幕吗?
  • @Yasuooooooooo 不,我收到此错误Undefined is not an object (evaluating this.props.navigation.goBack())。` "version": "0.62.2"`
  • 您是否将constructor(props) { } 放入了屏幕组件中?

标签: javascript react-native react-navigation


【解决方案1】:

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

【讨论】:

  • 添加相同。 "@react-navigation/stack": "^5.4.1",。但是遇到同样的错误Undefined is not an object (evaluating this.props.route.params)
  • 我已经发布了。
  • 您的实现似乎有问题。你可能错过了一些重要的点。我已经用一个工作示例更新了我的答案。 snack.expo.io/K1Kb_iTEW
【解决方案2】:

您正在尝试访问子类中的导航道具。为此,您需要使用withNavigation 高阶组件。

import { withNavigation } from 'react-navigation';
//    <YOUR COMPONENT>
export default withNavigation(YouChildComponent);

这为子组件提供了navigation 属性。你可以阅读更多here

希望对你有帮助。

【讨论】:

  • 我可以回去了。但问题是当我尝试添加这一行this.props.navigation.state.params.onNavigateBack(this.state.commentText); 。出现Undefined is not an object (evaluating 'this.props.navigation.state.params') 之类的错误
猜你喜欢
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多