【问题标题】:Cannot read property 'navigate of undefined无法读取属性“未定义的导航”
【发布时间】:2019-02-14 21:38:04
【问题描述】:

我尝试了我在网上看到的所有内容,但没有一个能解决我的问题。 它总是给我“无法读取未定义的属性'导航'。

我该如何解决这个问题?

我将信息从第 1 页发送到页眉,但我无法从页眉发送到第 1 页。以及如何从标题转到第 1 页我的意思是如何通过单击某些按钮打开第 1 页

import React, { Component } from 'react';
import { View, Text, FlatList } from 'react-native';
import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings(['Remote debugger']);

export default class Header extends Component {

    constructor(props){
        super(props);
    }

    render() {
        console.warn(this.props.navigation);
    return (
        <View style= {styles.headerStyle}>
            <View style= {[styles.View2, {backgroundColor: 'rgba(116,185,255,0.3)'}]} >
                <Text style={[styles.childText, {color:'#74b9ff'}]} >{this.props.color1}</Text>
            </View>
            <View style= {[styles.View2, {backgroundColor: 'rgba(255,234,167,0.3)'}]}>
                <Text style={[styles.childText, {color:'#ffeaa7'}]} >{this.props.color2}</Text>
            </View>
            <View style= {[styles.View2, {backgroundColor: 'rgba(204,255,204,0.3)'}]}>
                <Text style={[styles.childText, {color:'#ccffcc'}]} >0</Text>
            </View>
            <View style= {[styles.View2, {backgroundColor: 'rgba(255,128,128,0.3)'}]}>
                <Text style={[styles.childText, {color:'#ff8080'}]} >0</Text>
            </View>
            <View style= {[styles.View2, {backgroundColor: 'rgba(207,207,176,0.3)'}]}>
                <Text style={[styles.childText, {color:'rgba(207,207,176,0.3)'}]} >0</Text>
            </View>
        </View>
    );
 };
}

这是我使用页眉组件的第 1 页。

import React, { Component } from 'react';
import { View, Text, FlatList, Image, ScrollView} from 'react-native';
import DOMParser from 'react-native-html-parser';
import axios from 'axios';
import Header from './Header';
import Bar from './Bar';
import Footer from './Footer';

const Blue = [];
const Yellow = [];

export default class Page1 extends Component {    

    state = {
         leader: []
    }

    componentWillMount() {
        fetch('url')
        .then(response => {
            if (response.ok) {
                return response;
            }else {
                let error = new Error('Error ');
                error.response = response;
                throw error;
            }
            },
            error => {
                let errmess = new Error(error.message);
                throw errmess;
            })
        .then(response => response.text())
        .then(leaders => {
            const str = leaders.substring(76);
            const str2 = str.substring(0, str.length - 9);
            const x = JSON.parse(str2);
            this.setState({ leader: x });

        })
        .catch(error => {
            this.setState({ errMessage: error.message });
        });
        }

        renderall() {           
            return this.state.leader.map(alb => 
              <View style={styles.container} key= {alb.Ref}> 
                <Text style={[styles.textStyle, {marginLeft:'5%'}]}> {alb.Tescil_No}  </Text>

                <Text style={[styles.textStyle, {marginLeft:'6%'}]}> {alb.GumrukAdi}  </Text>

                <Text style={[styles.textStyle, { marginLeft:'5%'}]}> {alb.ACIKLAMA}   </Text>                                                
              </View>
          )
        }

        count(){
            return this.state.leader.map(color => {
                if(color.Renk == 'MAVI'){
                    Blue.push("MAVI");
                }
                else if(color.Renk == 'SARI')
                {
                    Yellow.push("SARI")
                }
            })
        }

    render() {

       this.count();

        console.log(Blue.length);

        console.log(this.state.leader);

        return (

            <View style= {styles.firstView}> 
                 <View style={{flex: 1.5}}>
                     <Header color1 = {Blue.length}  color2 = {Yellow.length}/>
                 </View >
                     <View style={{flex: 0.5, backgroundColor:'#f2f2f2'}}>
                    <Bar />
                    </View>
                <View style={{flex: 9}}>
                    <ScrollView>
                {this.renderall()}
                </ScrollView>
                </View>
                <View style={styles.footerStyle}>
                  <Footer />
                  </View>  
            </View>
        );
}

【问题讨论】:

  • 你是如何使用你的标题的?你能展示一下代码吗?
  • @Andrew 我想通过文本的 onpress 进入下一页。所以我不应该使用导航吗?
  • 你是在使用 redux 还是只是将 props 从一个组件传递到另一个组件?
  • 你在我的代码中看到“ {this.props.color1} " 我用 this.props.color1 获取数据。我想将这个类的值传递给另一个我获取数据“this.props.color1”的类
  • 我还想从这个页面转到另一个页面。可以帮我解决这两个问题

标签: javascript react-native


【解决方案1】:

问题是您没有将导航道具传递给 Header 组件。如果你像这样传递它们:

<Header color1 = {Blue.length}  color2 = {Yellow.length} navigation={this.props.navigation}/>

然后在您的 Header 组件中,您应该能够通过 this.props.navigation.navigate 访问它

这当然是您的Page1 包含在导航器中并且可以访问导航道具,否则您将不得不将它们传递给它。

这是一个小食,展示了如何在页面上使用标题组件构建基本导航

https://snack.expo.io/@andypandy/navigation-with-custom-header

代码如下:

注意Screen1.jsScreen2.js 都包含在导航器中,该导航器是在MainNavigation.js 中创建的。这允许他们访问导航道具。然后可以将这些 props 传递给 Screen1Screen2 内的子组件

App.js

import React, {Component} from 'react';
import AppContainer from './MainNavigation';
export default class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
    }
  }

  render() {
    return (
      <AppContainer />
    )
  }
}

MainNavigation.js

import Screen1 from './Screen1';
import Screen2 from './Screen2';
import { createStackNavigator, createAppContainer } from 'react-navigation';

const screens = {
  Screen1: {
    screen: Screen1
  },
  Screen2: {
    screen: Screen2
  }
}

const config = {
  headerMode: 'none',
  initialRouteName: 'Screen1'
}

const MainNavigator = createStackNavigator(screens,config);
export default createAppContainer(MainNavigator);

Header.js

import React, {Component} from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';

export default class Header extends React.Component {

  render() {
    console.log('props', this.props)
    return (
      <View style={styles.container}>
        <Button title={'Go to next screen'} onPress={() => this.props.navigation.navigate('Screen2',  {})} />
      </View>
    )
  }
}
const styles = StyleSheet.create({
  container: {
    height: 80,
    width: '100%',
    backgroundColor: '#006600',
    justifyContent: 'flex-end'
  }
});

Screen1.js

import React, {Component} from 'react';
import { View, StyleSheet, Text } from 'react-native';
import Header from './Header'

export default class Screen1 extends React.Component {


  render() {
    return (
      <View style={styles.container}>
        <Header navigation={this.props.navigation}/>
        <View style={{flex: 1}} />
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

Screen2.js

import React, {Component} from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';

export default class Screen2 extends React.Component {

  render() {
    return (
      <View style={styles.container}>
        <Text>New screen</Text>
        <Button title={'Go back'} onPress={() => this.props.navigation.goBack()}/>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

【讨论】:

  • 我做了你写的,在标题组件中我给了“ this.props.navigation.navigate('Page1')} style={[styles.childText , {color:'#74b9ff'}]} >{this.props.color1} "这是第一个文本。当我按文本​​时,它给了我同样的错误,例如“无法读取未定义的属性 'navigate'
  • 感谢@Andrew 的帮助,但我是 react-native 的新手。怎么能这样?
  • 是的,兄弟,非常感谢。我正在处理你的帖子。我会回到你身边:)
  • 我可以再问 1 个问题吗?当点击按钮时,Page1 是打开的,但在 2 或 3 秒后它又回到了上一页。你知道为什么吗?
  • 哦,好吧,我修好了:)
【解决方案2】:

您缺少构造函数。您需要使用构造函数然后this.props。添加这个:

export default class Header extends Component {
    constructor() {
        super(props)
    }
    render() { 
 //rest of your code

你从this.props.navigation 解构{navigate}。您的错误是因为 this.props.navigation 没有 navigate 属性。尝试console.log(this.props.navigation) 并检查是否有navigate 属性

【讨论】:

  • 您好,我更新了答案,我在您的问题上看到了一些新内容。谢谢,如果您检查过,请告诉我。
  • Actullay 我试着去下一页。在我的另一个页面中,我使用了这个“this.props.navigation.navigate('login');”所以它会自动进入“登录”页面。在这个页面中,我想给 navigate('Page1')} 去 page1。但它给了我这个错误
  • 我明白了,但是你有this.props.navigation中的导航功能吗?如果你在这里控制台登录this.props.navigation
  • 使用 console.log 时它给了我“未定义”
  • 你添加了构造函数吗?访问this.props ?以及如何将这些道具发送到您的 Header 组件?
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 2020-02-04
  • 2020-09-26
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
相关资源
最近更新 更多