StackNavigator can't nest multiple levels?

2019-06-14 02:19发布

I'm trying to learn how to use the stacknavigator for my react-native application. But the system keeps crashing once I'm at level 2 in the page hierarchy and I get the message:

Error while updating property 'accessibilityLabel' of a view manage by: RTCView

All my app does is present a word that says Region. If you click on Region, you'll see the word General. When you press the word General, you should see an empty screen, but instead, I get the error and crash mentioned above.

Here's the code to my simple project:

index.android.js

import React, { Component } from 'react';
import App from './components/Home';
import {
  AppRegistry,
  View
} from 'react-native';

export default class myapp extends Component {
  render() {
    return (
        <App />
    );
  }
}


AppRegistry.registerComponent('myapp', () => myapp);

components/Home.js

import React, { Component } from 'react';
import {StackNavigator} from 'react-navigation';
import Regions from './Regions';
import Compatibility from './Compatibility';

import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Linking
} from 'react-native';

class Home extends Component {
  static navigationOptions = {
    title: 'Login',
    headerStyle: {
        backgroundColor:'#000000'
            },
    headerTitleStyle: {
        color:'#fff'
    }
  };
  render(){
    const {navigate} = this.props.navigation;
    return (
      <View style={styles.container}>
        <Text style={styles.instructions} onPress={()=>navigate('Regions',{realm:'blah'})}>
            Regions
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },

  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});


const myscreens = StackNavigator({
  Home: {screen: Home},
  Regions:{screen:Regions},
  Compatibility:{screen:Compatibility}
});

export default myscreens;

components/Regions.js

import React, { Component } from 'react';
import {StackNavigator} from 'react-navigation';

import {
  Text,
  View,
  FlatList
} from 'react-native';

export default class Regions extends Component {
  static navigationOptions = {
    title: 'Pick Region',
    headerStyle: {
        backgroundColor:'#F00'
    },
    headerTitleStyle: {
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    },
    headerBackTitle:{
        color:'#fff'
    },
    headerBackTitleStyle:{
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    }
  };
    constructor(props)
    {
        super(props);
    }
  render() {


    const {navigate} = this.props.navigation;

    let data = [
    {regionName:'General',numOfDimensions:2}
    ];

    return (
        <FlatList
          data={data}
          keyExtractor={(item, index) => index}
          renderItem={({item}) => <Text onPress={()=>navigate('Compatibility',{item:item})}>{item.regionName}</Text>}
        />
    );

  }
}

components/Compatibility.js

import React, { Component } from 'react';

import {
  Text,
  View,
  FlatList
} from 'react-native';

export default class Compatibility extends Component {
  static navigationOptions = {
    title: 'Pick Region',
    headerStyle: {
        backgroundColor:'#F00'
    },
    headerTitleStyle: {
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    },
    headerBackTitle:{
        color:'#fff'
    },
    headerBackTitleStyle:{
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    }
  };

    constructor(props)
    {
        super(props);
    }

  render() {

    console.log('Compatibility');
    return <View></View>;
  }
}

What am I doing wrong? I just want to see the empty Compatibility screen, and get rid of this crashing.

1条回答
对你真心纯属浪费
2楼-- · 2019-06-14 02:47

There is no problem with the react navigation. You can go nested using the react navigation.I have used this and its working fine. When i tested your code i found that there is a mistake you are making inside your code which produces this error not the react navigation. In your code for the Regions class inside your navigation options, you just declared a object style in the prop which takes the title with a string. For more clarification check the code below:-

static navigationOptions = {
    title: 'Pick Region',
    headerStyle: {
        backgroundColor:'#F00'
    },
    headerTitleStyle: {
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    },   

headerTruncatedBackTitle =======>>>>> this is the title which accepts only string, This is not a style for the header truncated back title

    headerBackTitle:{
        color:'#fff'
    },   

headerBackTitle =======>>>>> this is the title which accepts only string, This is not a style for the header back title

    headerBackTitleStyle:{
        color:'#fff'
    },
    headerTruncatedBackTitle:{
        color:'#fff'
    }
};

I just ran your code and its working fine after correcting the things. Let me know if you still have any doubts :)

查看更多
登录 后发表回答