【问题标题】:undefined is not an object (evaluating '_this2.props.navigation.navigate')undefined 不是对象(评估 '_this2.props.navigation.navigate')
【发布时间】:2017-05-17 09:16:18
【问题描述】:

点击标题时,我希望它导航到下一页。但是,我不知道如何在课堂外访问导航道具。有关如何执行此操作的任何建议?

import React,{Component} from 'react'
import { View, Text, TouchableOpacity, FlatList} from 'react-native'

class header extends Component {
  render(){
    return(
        <TouchableOpacity
          onPress={() => **this.props.navigation.navigate('PageTwo')**}
        >
          <Text>{'Go to next Page Two'}</Text>
        </TouchableOpacity>
    )
  }
}

export default class PageOne extends Component {
  static navigationOptions = {
    title: 'Page One',
  }
  constructor(props) {
    super(props);
    this.state = {
      data: // ...
    };
  }

  _renderItem = ({item}) => (
    // ...
  );

  render(){
    return(
        <FlatList
          ListHeaderComponent={header}
          data={this.state.data}
          renderItem={this._renderItem}
        />
    )
  }
}

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    对于 React 导航 首先确保您在项目中安装了 react-navigation 库。 如果没有,则在 cmd 中运行“npm install --save react-navigation”。 然后创建一个 App.js 文件来保存所有路由器名称,如

    App.js:

    import React from 'react';
    import {
      View,
      Text,
      StyleSheet,
    } from 'react-native';
    import {StackNavigator} from 'react-navigation';
    
    import Splash from './src/components/Splash/Splash'
    import Login from './src/components/Splash/Login'
    
    const Navigation = StackNavigator({
      Splash : {
        screen : Splash
      },
      Login : {
        screen : Login
      },
    })
    export default Navigation;
    

    在我的情况下,src 是存储所有 js 文件的污点。

    现在在 index.android.js 中设置 AppRegistry.registerComponent。

    index.android.js

    import React, { Component } from 'react';
    import {
      AppRegistry,
      Text,
      View
    } from 'react-native';
    import {StackNavigator} from 'react-navigation';
    import Splash from './src/components/Splash/Splash'
    import Navigation from './App'
    export class FirstReactDemo extends Component {
      render() {
        return (
          <Splash/>
        );
      }
    }
    AppRegistry.registerComponent('FirstReactDemo', () => Navigation);
    

    现在,在第一个屏幕中设置 onclick 监听器。

    Splash.js

    import React, { Component } from 'react';
    import {
      View,
      Text,
      Button,
      StyleSheet,
    } from 'react-native';
    import {StackNavigator} from 'react-navigation';
    
    export default class SplashScreen extends Component {
    
    
    
    static navigationOptions = {
          title: 'Splash',
        };
      render() {
        const { navigate } = this.props.navigation;
        return (
          <View>
            <Text
              onPress={() =>navigate('Login')}
              style={styles.text}>My Splash screen</Text>
          </View>
        );
      }
    }
    

    为下一个屏幕创建 Login.js 文件。

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      在使用您的子组件时自己传递navigation = {navigation}

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多