React(v4) - 使用withRouter导出组件会返回错误

时间:2017-08-30 17:02:35

标签: reactjs react-router react-router-v4

根据另一篇文章React-Router v4: Cannot read property 'route' of undefined,这个问题可能是一个错误,显然这个人通过将他们的路由器包装在browserRouter中解决了他们的问题。但是,我已经这样做了,似乎仍然得到了错误。就像其他帖子一样,当我使用withRouter导出我的组件时,它会返回一个错误,但是当我在没有它的情况下导出它时,它可以正常工作。我想要做的是,找到一个人加载路由时的路径名,检查它们是否已经过身份验证,如果没有,则重定向到登录,如果是,则重定向到主页。我已经有这个问题了一段时间了,如果你知道解决方案请帮助我。

Routes.js

import { Meteor } from "meteor/meteor"
import React from "react";
import { withRouter, Switch, BrowserRouter, Route, Redirect, Link } from "react-router-dom";
import { Tracker } from "meteor/tracker";

import Login from "../ui/authentication/Login";
import Signup from "../ui/authentication/Signup";
import Home from "../ui/Home";
import searchNotes from "../ui/searchNotes"
import Note from "../ui/Note";
import fullSize from "../ui/fullSize"
import userProfile from "../ui/userProfile";
import AddNote from "../ui/AddNote";
import questions from "../ui/questions"
import NotFound from "../ui/NotFound";

  export default class Routes extends React.Component{
  render(){
    const signedIn = Meteor.userId();
    const UnauthPage = ["/login", "/signup"];
    console.log(this.props)
    // if(signedIn && ){
    //   console.log("Signed in")
    // }
    return (
        <BrowserRouter>
          <Switch>
            <Login path="/login" />
            <Signup path="/signup" />
            <Route path="/" component={Home} exact/>
            <Route path={`/searchNotes/:subject`} component={Note} />
            <Route path={`/searchNotes`} component={searchNotes} />
            <Route path={`/fullSize/:noteId`} component={fullSize}/>
            <AddNote path="/addNote"/>
            <Route path="/questions" component={questions} />
            <Route component={userProfile} path={`/users/:userId`} />
            <NotFound />
          </Switch>
        </BrowserRouter>
    )
  }
}

0 个答案:

没有答案