【问题标题】:Problem with react-router-dom while importing plain router from it从它导入普通路由器时 react-router-dom 出现问题
【发布时间】:2020-08-20 12:13:38
【问题描述】:

我需要为我创建此文件创建自定义历史记录

import createHistory from "history/createBrowserHistory";

export default createHistory();

并将其导入我的 App.js 文件中,如下所示:

import React from "react";
import { Router, Route } from "react-router-dom";

import StreamList from "./streams/streamList";
import StreamCreate from "./streams/streamCreate";
import StreamDelete from "./streams/streamDelete";
import StreamEdit from "./streams/streamEdit";
import StreamShow from "./streams/streamShow";
import Header from "./header";
import history from "../history";

function App() {
  return (
    <div className="ui container">
      <Router hitory={history}>
        <Header />
        <Route path="/" exact component={StreamList} />
        <Route path="/streams/new" component={StreamCreate} />
        <Route path="/streams/edit" component={StreamEdit} />
        <Route path="/streams/delete" component={StreamDelete} />
        <Route path="/streams/show" component={StreamShow} />
      </Router>

并收到此错误:

TypeError: 无法读取未定义的新路由器 E:/xord/streams/modules/Router.js:20 17 | 的属性“位置”超级(道具); 18 | 19 | this.state = { > 20 |位置:props.history.location

【问题讨论】:

    标签: javascript reactjs navigation react-router-dom


    【解决方案1】:

    问题很可能是&lt;Router /&gt;&lt;Route /&gt; 组件之间缺少&lt;Switch /&gt; 组件。从&lt;Switch /&gt;的文档看:

    呈现与位置匹配的第一个子 &lt;Route&gt;&lt;Redirect&gt;

    尝试添加如下:

    <Router hitory={history}>
        <Header />
        <Switch>
            <Route path="/" exact component={StreamList} />
            <Route path="/streams/new" component={StreamCreate} />
            <Route path="/streams/edit" component={StreamEdit} />
            <Route path="/streams/delete" component={StreamDelete} />
            <Route path="/streams/show" component={StreamShow} />
        </Switch>
    </Router>
    

    【讨论】:

    • 谢谢。如果发现任何问题,我们会尝试并回复您。
    • 嗨,我按照你说的做了同样的事情,但仍然遇到同样的错误。
    • 还有其他人想帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 2019-03-17
    • 2019-01-15
    • 2022-06-15
    • 2022-01-09
    • 2020-10-14
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多