javascript - undefined 不是一个函数(评估'_this.props.navigation.navigate')

标签 javascript reactjs react-native

我正在尝试从 Form.js 访问“主页”。但是我的代码显示了那个错误。我该如何解决?

我在 arrowF、routes 中尝试过,但没有任何效果。同样的错误...

This is my Form.js where I call onPress=() I tried function, navigation and nothing works. I try add const { navigation } = this.props; and nothing works. Same error.

<View style={styles.viewChild}>
  <TouchableHighlight
     onPress={this._Home} title="Home"
     style={styles.button} >
   <Text style={{color: '#fff', fontSize: 16}}>Acessar</Text>
 </TouchableHighlight> 

This is my Login.js. Where I call Component

import React from 'react';
import {
  Container, Top, Logo, Title, Content
} from './styles';

import Form from '~/components/Form/form';
import logo from '~/assets/logo.png';
import { MaterialTopTabBar, navigation } from 'react-navigation';

export default function Login(){
  return (
    <Container>
      <Top>
        <Logo source={logo} />
        <Title
          style={MaterialTopTabBar.display1}
        >Welcome</Title>
      </Top>
      <Content>
        <Form />
      </Content>
    </Container>
  );
}

This is my Route.js I tried changes between Stack, BottomNav, and nothing works. The Route from Form.js to 'Home' failed. I can post my GitHub.

import { createAppContainer, createSwitchNavigator, createStackNavigator
} from 'react-navigation';

import Form from '~/components/Form/form';
import Home from '~/pages/Home/home';
import Main from '~/pages/Main';

const Routes = createAppContainer(
  createSwitchNavigator({
    Main: {screen: Main},
    Form: {screen: Form},
    Home: {screen: Home,
    navigationOptions: {
      title: 'Home',
      headerBackTitle: null,
    }}
   }),
);

export default Routes;

最佳答案

函数使用如下:

func1(){
    this.props.navigation.navigate("Form");
}
...
 <TouchableHighlight
     onPress={this.func1.bind(this)} title="Home"

or

 <TouchableHighlight
     onPress={() => this.func1()} title="Home"

使用箭头函数

const func1 = () => {
    this.props.navigation.navigate("Form");
}
...
 <TouchableHighlight
     onPress={this.func1} title="Home"

关于javascript - undefined 不是一个函数(评估'_this.props.navigation.navigate'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57294869/

相关文章:

javascript - 从 CasperJS 调用 Facebook API?

javascript - 当 url 包含单引号时,如何使用 nock 和 request-promise 测试路由?

reactjs - 如何显示从 fetch() 方法返回的图像 - react-native

reactjs - 与 React js 集成时,列不使用 Gridjs 显示数据

react-native - 在 react-native 中注册组件之前等待异步

reactjs - 如何在用于设置状态值的同一函数中访问更新的状态值

javascript - 无法在 JQuery 终端中编辑文本区域

javascript - 使用 jquery 制作 div 动画

node.js - 如何将 React 应用程序部署到 Prod 以及如何管理版本控制

javascript - 使用 props 设置图片 src