【问题标题】:Issue with StackNavigator: Route 'X' should declare a screenStackNavigator 的问题:Route 'X' 应该声明一个屏幕
【发布时间】:2017-03-26 21:32:53
【问题描述】:

具体错误是“Route 'Camera' should declare a screen”。

我有 index.js,它导入 Camera.js 并渲染相机组件。 Camera.js 导入 router.js,它导入我所有的屏幕并创建一个 StackNavigator。我是 React 新手,所以可能有些东西我不理解。这是代码...

Index.js

import React, { Component } from 'react';
import { CameraView } from './screens/Camera'

class App extends Component {
  render() {
    return <CameraView />;
  }
}

export default App;

Camera.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
} from 'react-native';
import { AppStack } from '../config/router';

....

export default class CameraView extends Component {
    viewProducts = () => {
        this.props.navigation.navigate('Products');
    };
    render() {
        const { navigate } = navigation;
        return (
           <View style={styles.container}>
           <Camera 
             ref={(cam) => {
               this.camera = cam;
             }}
             style={styles.preview}
             aspect={Camera.constants.Aspect.fill}
             onBarCodeRead={this.readBarcode}>
             <Button
               onPress={() => this.viewProducts}
               title="Products"
             />
          </Camera>
        </View>
      );
   }
}

Router.js

import React from 'react';
import { StackNavigator } from 'react-navigation';
import { Icon } from 'react-native-elements';

import Products from '../screens/Products';
import ProductDetail from '../screens/ProductDetail';
import CameraView from '../screens/Camera';

export const Root = StackNavigator({
  Camera: {
    screen: CameraView,
  },
  ProductDetail: {
    screen: ProductDetail,
    navigationOptions: {
      title: ({ state }) => `${state.params.name}}`
    },
  },
  Products: {
    screen: Products,
    navigationOptions: {
      title: 'Products',
    },
  },
});

我在这里导入 CameraView 并将其用作我的相机路线的屏幕。 CameraView 不被视为屏幕吗?谢谢。

【问题讨论】:

  • 如果有人能提供帮助,仍在寻找答案。

标签: javascript reactjs react-native react-router


【解决方案1】:

解决了问题。而不是渲染

render() {
    return <CameraView />;
} 

在我的 index.js 中,我需要导入 router.js 并渲染

render() {
    return <Root />;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多