0
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 | */ | function NativeRouter(props) {

返回 <MemoryRouter {...props} />; | } |"**

4

1 回答 1

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>
         
于 2020-07-14T14:42:20.540 回答