简体   繁体   English

react-router 的未安装组件

[英]Unmounted component with react-router

I have a problem with my react router when I want to pass from a router to another.当我想从一个路由器传递到另一个路由器时,我的反应路由器有问题。 I have a Connexion files:我有一个连接文件:

import React from "react";
import {
  Switch,
  BrowserRouter as Router,
  Route,
  Redirect,
} from "react-router-dom";
import SignIn from "./Containers/Login/Login";
import Player from "./Player";

const Routes = () => {
  return (
    <Router>
      <Switch>
        <Route exact path="/" component={SignIn} />
        <Route path="/app/player/" component={Player} />
        <Route path="/*">
          <Redirect to="/" />
        </Route>
      </Switch>
    </Router>
  );
};

export default Routes;

When I connect from "SignIn", I want to go in the next route "Player":当我从“登录”连接时,我想在下一条路线“播放器”中连接 go:

import React from "react";
import {
  Switch,
  BrowserRouter as Router,
  Route,
  useRouteMatch,
} from "react-router-dom";
import Navigation from "./Navigation/PlayerNavigation";

import FillCollect from "./Containers/Collect/Collect";
import Dashboard from "./Containers/Dashboard/Dashboard";

const Player = () => {
  const { path } = useRouteMatch();
  console.log(path);
  return (
    <Router>
      <Navigation />
      <Switch>
        <Route path={`${path}/collect/:id`} component={FillCollect} />
      </Switch>
    </Router>
  );
};

export default Player;

(My Navigation component is my sidebar and topbar) (我的导航组件是我的侧边栏和顶栏)

But when I'm connect my Navigation is displayed but not my Fill Collect: And i have this warning :但是当我连接时,我的导航会显示,但我的填充收集没有显示:我有这个警告:

Warning: Can't perform a React state update on a unmounted component.警告:无法对未安装的组件执行 React state 更新。 This is a no-op, but it indicates a memory leak in your application.这是一个无操作,但它表明您的应用程序中存在 memory 泄漏。 To fix, cancel all subscriptions and asynchronous tasks in a useEffect clean function.要修复,请取消 useEffect clean function 中的所有订阅和异步任务。 In SignIn (created by Context.Consumer)登录登录(由 Context.Consumer 创建)

I tried to display a minimalist component with just div but it's the same我试图用 div 显示一个极简主义的组件,但它是一样的

There are a couple of problems in this这有几个问题

  • Firstly you are using a nested Router component too, which then restricts the inner Routes from listening to the router Router component首先,您也使用嵌套的路由器组件,然后限制内部路由侦听路由器路由器组件
  • Secondly, your matchPath in parent is "/app/player/" which if you combine with the nested Route in child like ${path}/collect/:id .其次,您在父项中的matchPath"/app/player/" ,如果您与子项中的嵌套路由(例如${path}/collect/:id结合使用。 It becomes "/app/player//collect/:id" which is not what you indent它变成"/app/player//collect/:id" ,这不是你缩进的

The working solution will be as follows工作解决方案如下

const Routes = () => {
  return (
    <Router>
      <Switch>
        <Route exact path="/" component={SignIn} />
        <Route path="/app/player" component={Player} />
        <Route path="/*">
          <Redirect to="/" />
        </Route>
      </Switch>
    </Router>
  );
};

export default Routes;

const Player = () => {
  const { path } = useRouteMatch();
  console.log(path);
  return (
    <React.Fragment>
      <Navigation />
      <Switch>
        <Route path={`${path}/collect/:id`} component={FillCollect} />
      </Switch>
     </React.Fragment>
  );
};

export default Player;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM