我正在尝试为我的React Native应用中的不同页面制作路线。所以我用新的路由器创建了一个router.js文件:
// @flow
import React, { Component } from 'react';
import View from 'react-native';
import { NativeRouter, Route, Link } from 'react-router-native'
import PropTypes from 'prop-types';
import Splash from './components/Splash';
import Home from './components/Home';
class Router extends Component {
componentWillMount() {
navigator = this.context.history;
}
/**
* Render
* @return {XML} JSX
*/
render() {
return (
<View style={{flex: 1}}>
<Route exact path="/" component={Splash}/>
<Route exact path="/home" component={Home}/>
</View>
);
}
}
export default Router;
然后我在Splash Component中导入了路由器。从那里我想要一个按钮将用户重定向到主屏幕。
import React, { Component } from 'react';
import { View, Text, Image, Button } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
import { NativeRouter, Route, Link, Switch } from 'react-router-native'
import Home from "./Home";
import Router from "../router";
class Splash extends Component {
render() {
return(
<View style={ styles.container }>
<Router>
<Button onPress={() => navigator.push('/home')}
title="Go to home" />
</Router>
</View>
);
}
}
export default Splash;
但是我收到以下错误:
Invariant Violation: Element type is invalid expected a string (for built-in components) or a class/function (for composite components) but got: object
Check the render method of 'Router'
这是我第一次使用React Native Router而且我没有使用它。有人知道使一个简单的路由器在屏幕之间导航的最佳解决方案吗?
答案 0 :(得分:0)
它似乎是你传递的组件。在文档中:
https://github.com/jmurzy/react-router-native
组件就是这样完成的:
const Detail = (props) => (
<View style={[styles.component, { backgroundColor: '#FFFFFF' }]}>
{props.children}</View>
);
您似乎正在导入文件并将引用传递给该文件。尝试按照上述文档进行操作,看看是否能解决问题。