简体   繁体   English

React Nested Router仅更改url,但不呈现页面

[英]React Nested Router only change the url, but doesn't render the page

I am new here and writing to ask a problem about React's router. 我是新来的,正在写信问关于React路由器的问题。 I have Googled for over two hours but cannot find a working solution. 我已经用Google搜索了两个多小时,但找不到有效的解决方案。

Basically, I am writing a website that can show a list of articles. 基本上,我正在写一个可以显示文章列表的网站。 There is a button called "Articles" on the homepage. 主页上有一个名为“文章”的按钮。 Once we click it, the router will work and go to page "/articles", like the codes below show. 一旦单击它,路由器将工作并转到页面“ / articles”,如下面的代码所示。

File A: 档案A:

import React, { Component } from 'react';
import {BrowserRouter, Route, Link, Switch} from 'react-router-dom';

class A extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (
            <BrowserRouter>
            <div>
                <div>
                    <button><Link to = "/articles"> Articles </Link></button>
                </div>
                <Switch>
                    <Route exact path = '/articles' render = {() => <B />}/>
                </Switch>
            </div>
            </BrowserRouter>
        );
    }
}

The list of articles will be shown by the component B. I designed that component B will have two situations. 文章列表将由组件B显示。我设计了组件B有两种情况。 First, by default, when we click the button in component A, the URL is "/articles", and then a list of article shows. 首先,默认情况下,当我们单击组件A中的按钮时,URL为“ / articles”,然后显示文章列表。 Second, if we click any of the article title, the URL will be changed to "articles/articleID", and the component will show the detail of that articleID. 其次,如果我们单击任何文章标题,则URL将更改为“ articles / articleID”,并且组件将显示该articleID的详细信息。

File B: 档案B:

import React, {Component} from 'react';
import {BrowserRouter, Route, Link, Switch} from 'react-router-dom';

class B extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <BrowserRouter>
                <div>
                        <Switch>
                            <Route exact path = "/articles" render = {(props) => <Articles {...props} />}/>
                            <Route path = "/articles/:articleId" render = {() => <ArticleDetail />}/>
                        </Switch>
                </div>
            </BrowserRouter>
        );
    }
}

Now my problem is, when in the page of article details (when the page URL is "/articles/articleId"), I can click the "Articles" button in component A, and the URL will be changed to "/articles", but the page will not be rendered, which means, I will still on the page of article details, instead of returning to the page of a list of articles. 现在我的问题是,当在文章详细信息页面中(页面URL为“ / articles / articleId”)时,我可以单击组件A中的“文章”按钮,并且该URL将更改为“ / articles”,但页面不会被渲染,这意味着,我仍将在文章详细信息页面上,而不是返回到文章列表页面。

If my description confuses you, please ask me! 如果我的描述使您感到困惑,请问我! Thank you for your help! 谢谢您的帮助!

我认为您必须仅在主要应用程序组件(如“ A”)中使用开关组件,并在每个“ Route”上使用“ exact”关键字,请查看react-router v4以获取更多信息: https : //reacttraining.com/react-路由器/网络/ API /开关

Hey I don't know if you still have this problem, but try making <Route exact path = '/articles' render = {() => <B />}/> not exact. 嘿,我不知道您是否仍然遇到此问题,但是请尝试使<Route exact path = '/articles' render = {() => <B />}/>不精确。

I had this same problem, and I believe when you navigate to /articles/:articleId the path doesn't match /articles anymore so your component isn't rendered anymore. 我遇到了同样的问题,我相信当您导航到/articles/:articleId ,路径不再与/articles匹配,因此不再呈现您的组件。

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

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