import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import Login from "./components/Login";
import Home from "./components/Home";
import { NativeRouter, Switch, Route, Link } from "react-router-native";
im;
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {},
};
}
componentDidMount() {
this.authListener();
}
authListener() {
fire.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({ user });
} else {
this.setState({ user: null });
}
});
}
render() {
return (
<NativeRouter>
<View style={styles.container}>
<Switch>
<Route exact path="/">
{this.state.user ? <h1><Home/></h1> : <h1><Login/></h1>}
</Route>
</Switch>
</View>
</NativeRouter>
);
}
}
**在上面的代码中,我正在使用Firebase身份验证。如果用户已登录,则我需要将该应用程序路由到其他应用程序组件,将该应用程序路由到“登录”组件。我正在使用react-router-native作为npm包来路由到这里。但我收到错误消息,因为“ D:/csearch-client/csearch-client/node_modules/react-router-native/NativeRouter.js 11:9 模块解析失败:意外的令牌(11:9) 您可能需要适当的加载程序来处理此文件类型,当前没有配置任何加载程序来处理此文件。参见https://webpack.js.org/concepts#loaders | * / |函数NativeRouter(props){
返回
; | } |“ **
答案 0 :(得分:0)
Ciao, 我认为您可以执行以下操作:
import Login from 'path/to/Login';
import Home from 'path/to/Home';
...
<Switch>
<NativeRouter exact path="/" component={this.state.user ? Home : Login}>
</NativeRouter>
</Switch>