简体   繁体   English

我在本机反应中与此this.props.navigation.navigate有问题

[英]I am having issues with this.props.navigation.navigate in react native

I have been trying to create a touchable highlight that takes to me a new scene by using this.props.navigation.navigate for a react-native app. 我一直在尝试通过将this.props.navigation.navigate用于本机应用程序来创建一个可触摸的亮点,将我带入一个新的场景。 However, react native gives me an error that says undefined is not an object (evaluating '_this2.props.navigation'). 但是,react native给我一个错误,指出undefined不是对象(评估'_this2.props.navigation')。 Here is my code: 这是我的代码:

import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
View,
ListView,
Image,
Text,
ScrollView,
TouchableHighlight
} from 'react-native';
import { DrawerNavigator } from 'react-navigation';
import { List, ListItem } from 'react-native-elements';
import beerlist from '../datbeerlist.json';
import BeerDetails from './BeerDetails';

const squadIcon = require('../../img/serioussquad.jpg');

class BeerList extends Component {
 constructor(props) {
 super(props);
 var ds = new ListView.DataSource({
   rowHasChanged: (r1, r2) => r1 !== r2
 });

 this.state = {
 dataSource: ds.cloneWithRows(beerlist),
 };

 }


 renderRow(beer) {

return (
  <View style={styles.row}>
    <Image style = {styles.icon} source  = {squadIcon}/>
    <View style={styles.info}>
    <TouchableHighlight onPress={() => this.props.navigation.navigate('BeerDetails')}>
      <Text style={styles.items}>
      {beer.name} 
      </Text>
      </TouchableHighlight>
    </View>
    <View style={styles.total}>
      <Text style={styles.price}>${(beer.price / 100).toFixed(2)}</Text>
    </View>
  </View>
);
}

Any ideas on how to get it fixed? 关于如何解决它的任何想法?

Although @justelouise is right and the 尽管@justelouise是正确的,并且

StackNavigator Definiton StackNavigator Definiton

part of the code would help, still, if you've navigated to this page using stack navigator then it's been added to the stick navigator already. 如果您已使用堆栈导航器导航到此页面,则部分代码仍会有所帮助,然后它已被添加到stick导航器中。

I think this.props.navigation is available under the render() method. 我认为this.props.navigationrender()方法下可用。 If you want to use it in another method, then you have to pass it from render() to the method you want. 如果要在其他方法中使用它,则必须将其从render()传递到所需的方法。 Something like this: 像这样:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  View,
  ListView,
  Image,
  Text,
  ScrollView,
  TouchableHighlight
} from 'react-native';
import { DrawerNavigator } from 'react-navigation';
import { List, ListItem } from 'react-native-elements';
import beerlist from '../datbeerlist.json';
import BeerDetails from './BeerDetails';

const squadIcon = require('../../img/serioussquad.jpg');

class BeerList extends Component {
  constructor(props) {
    super(props);
    var ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2
    });

    this.state = {
      dataSource: ds.cloneWithRows(beerlist),
    };
  }


  renderRow (beer, renderNavigation) {
    return (
      <View style={styles.row}>
        <Image style={styles.icon} source={squadIcon}/>
        <View style={styles.info}>
          <TouchableHighlight onPress={() => renderNavigation.navigate('BeerDetails')}>
            <Text style={styles.items}>
              {beer.name} 
            </Text>
          </TouchableHighlight>
        </View>
        <View style={styles.total}>
          <Text style={styles.price}>${(beer.price / 100).toFixed(2)}</Text>
        </View>
      </View>
    );

  render () {
    const navigation = this.props.navigation;
    return (
      <View>
        {this.renderRow(BEER, navigation)}
      </View>
    )
  }
}

If you are navigating to stackNavigator outside of render() then try this 如果要导航到render()之外的stackNavigator ,请尝试以下操作

static navigationOptions = ({ navigation })=>{
        const { navigate } = navigation
        return{
            title: 'Header Text',
            headerRight:(<Button 
            title="Settings" backgroundColor="rgba(0,0,0,0)" color="rgba(0,122,255,1)"
            onPress={() =>navigate('settings')}
        />)
        } 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native - 导航问题“未定义不是对象(this.props.navigation.navigate)” - React Native - navigation issue “undefined is not an object (this.props.navigation.navigate)” 反应本机导航(未定义不是对象.. this.props.navigation.navigate - React native navigation (undefined is not an object .. this.props.navigation.navigate 在 React Native Button 中未定义 this.props.navigation.navigate('Form') - Undefined this.props.navigation.navigate('Form') in React Native Button undefined 不是对象 this.props.navigation.navigate 反应原生 - undefined is not an object this.props.navigation.navigate react native 【React-native】undefined不是对象(评估&#39;_this.props.navigation.navigate&#39;) - 【React-native】undefined is not an object (evaluating '_this.props.navigation.navigate') undefined 不是一个对象(评估&#39;this.props.navigation.navigate&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation.navigate') - React Native 未定义(评估&#39;this.props.navigation.navigate&#39;)React Native - undefined (evaluating 'this.props.navigation.navigate') React Native Undefined 不是 React Native 上的 object(评估“this.props.navigation.navigate”) - Undefined is not an object (evaluating 'this.props.navigation.navigate') on React Native React Native:React Navigation StackNavigator 不工作。 得到错误:“未定义不是一个对象(评估&#39;this.props.navigation.navigate&#39;)” - React Native: React Navigation StackNavigator not working. Getting error: “undefined is not an object (evaluating 'this.props.navigation.navigate')” react-native-navigation给出错误“未定义不是对象(评估&#39;_this.props.navigation.navigate&#39;)” - react-native-navigation giving error “undefined is not an object (evaluating '_this.props.navigation.navigate')”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM