试图回到“/"在反应路由器? [英] Trying to go back to "/" in react router?

查看:32
本文介绍了试图回到“/"在反应路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要的是在 App.js 中有一个顶级路由,它路由到Home 在/"上.在 Home 中,我想渲染一些东西,然后在一个地方根据路径选择要渲染的内容.

即如果路径是/"我想显示一个 Link 可以带我到/about",如果路径是/about"我会显示 About 组件在那里.

App.js 中,我总是有一个 Link 可以带我回到/".

<小时>

所以 App.js 渲染这个:

 <路由器><div><链接到="/"><button>转到主页</button></链接><路由精确路径="/" component={() =><主页/>}/><路由精确路径="/other" component={() =><其他/>}/>

</路由器>

Home 渲染这个:

 

这是家哇!<路由器><div><路由精确路径="/" component={() =><HomeController/>}/><路由精确路径="/about" component={() =><关于/>}/>

</路由器>

HomeController 渲染这个:

 <button>转到关于</button></链接>

About 渲染这个:

 

关于

<小时>

当我启动应用程序时,它看起来像这样:

当我点击转到关于"时,它看起来像这样:

正确更新url和路由器在Home

中显示的内容

但如果我现在点击回家",就会发生这种情况:

在渲染 Home 时是否正确更新了 url 但保留了关于"页面?

<小时>

这是为什么?为什么/"似乎仍然路由到/about"?我需要更改什么才能使按钮路由回到/"并再次显示转到关于"按钮?

这是我用来重现问题的所有代码:pastebin

解决方案

您需要更正一些问题.

首先,您的应用中必须只有一个路由器,而不是嵌套路由器

第二,如果您在父路由上有一个确切的关​​键字,那么嵌套路由将不匹配,因为匹配将在父级本身失败

第三,那么你不想把自定义的 props 传递给子组件,你必须像 component={Home} 而不是 component 一样渲染它们={() =><Home/>} 并且如果你想将 props 传递给孩子,你必须使用 render 而不是 component prop 并编写 render={(道具)=><Home test="1" {...props}}

您的完整代码如下

import React, { Component } from "react";从react-dom"导入{渲染};从react-router-dom"导入{ BrowserRouter 作为路由器、路由、链接};类 App 扩展组件 {使成为() {返回 (<div className="应用程序"><路由器><div><链接到="/"><button>转到主页</button></链接><Route path="/" component={Home}/><路由精确路径="/other" component={Other}/>

</路由器>

);}}类 Home 扩展组件 {使成为() {返回 (<div className="home"><div><路由精确路径="/" component={HomeController}/><路由精确路径="/about" component={About}/>

);}}类 HomeController 扩展组件 {使成为() {返回 (<div className="homecontroller"><链接到="/关于"><button>转到关于</button></链接>

);}}类关于扩展组件{使成为() {return <div className="about">ABOUT</div>;}}类其他扩展组件{使成为() {返回 <div className="other">OTHER</div>;}}render(, document.getElementById("root"));

工作演示

您可以参考以下问题了解更多详情

将自定义道具传递给路由器react-router v4 中的组件

反应路由器 4 嵌套路由不渲染

So what I want is to have a top-level routing in App.js that routes to Home on "/". In Home i want to render a few things and then one place where I chose what to render based on the path.

i.e. if the path is "/" I want to show a Link that can take me to "/about", if the path is "/about" I'll show the About component there.

In App.js I always have a Link that can take me back to "/".


So App.js render this:

  <Router>
    <div>          
      <Link to="/">
        <button>Go to home</button>
      </Link>
      <Route exact path="/" component={() => <Home/>} />
      <Route exact path="/other" component={() => <Other/>} />
    </div>
  </Router>

Home render this:

  <div>
    THIS IS HOME WOO!
    <Router>
      <div>
        <Route exact path="/" component={() => <HomeController/>} />
        <Route exact path="/about" component={() => <About/>} />
      </div>
    </Router>
  </div>

HomeController render this:

  <Link to="/about">
    <button>Go to about</button>
  </Link>

and About render this:

  <div>
    ABOUT
  </div>


When I start the app it looks like this:

When I click 'Go to about' it looks like this:

correctly updating the url and what is showed by the router in Home

But if I now click on 'Go to home' this happens:

correctly updating the url but keeping the 'about' page when rendering Home?


Why is this? Why does "/" seem to still route to "/about"? What would I need to change to make the button route back to "/" and show the 'Go to about'-button again?

Here is all the code I used to recreate the issue: pastebin

解决方案

There are a few things that you need to correct.

First, you must have just a single Router in your App and not nested Router

Second, If you have an exact keyword on the parent Route then the nested Routes won't match since the match will fails at the parent itself

Third, Then you don't want to pass custom props to the child component, you must render them like component={Home} and not component={() => <Home/>} and if you want to pass props to children, you must use render and not component prop and write render={(props) => <Home test="1" {...props}}

Your complete code will look like

import React, { Component } from "react";
import { render } from "react-dom";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

class App extends Component {
  render() {
    return (
      <div className="App">
        <Router>
          <div>
            <Link to="/">
              <button>Go to home</button>
            </Link>
            <Route path="/" component={Home} />
            <Route exact path="/other" component={Other} />
          </div>
        </Router>
      </div>
    );
  }
}

class Home extends Component {
  render() {
    return (
      <div className="home">
        <div>
          <Route exact path="/" component={HomeController} />
          <Route exact path="/about" component={About} />
        </div>
      </div>
    );
  }
}

class HomeController extends Component {
  render() {
    return (
      <div className="homecontroller">
        <Link to="/about">
          <button>Go to about</button>
        </Link>
      </div>
    );
  }
}

class About extends Component {
  render() {
    return <div className="about">ABOUT</div>;
  }
}

class Other extends Component {
  render() {
    return <div className="other">OTHER</div>;
  }
}

render(<App />, document.getElementById("root"));

Working demo

You can refer the following question for more details

Passing custom props to router component in react-router v4

React Router 4 Nested Routes not rendering

这篇关于试图回到“/"在反应路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆