【问题标题】:React component not rendering on route changed反应组件未在路线上呈现更改
【发布时间】:2018-11-11 06:33:29
【问题描述】:

我正在尝试通过相同的路由使用默认渲染组件渲染父组件子组件。

例如

可以通过此路由“/dash/overview”访问 ParentComponent 现在在 ParentComponent 中,我想渲染一个与 ParentComponent 具有相同路由的默认组件。

父组件.js

                <Switch>
                  <Route path="/dash/overview/" render={() => <SubComponentOne/>}/>
                  <Route path="/dash/subtwo" render={() => <SubComponentTwo/>}/>
                </Switch>

我想渲染 SubComponentOne.. 它工作正常,但是当我将路由更改为 /dash/subtwo 时,它不会渲染 SubComponentTwo

我正在使用路由器Link 在路由之间切换,我也在使用HashRouter

【问题讨论】:

  • 控制台有错误吗?
  • 浏览器控制台没有错误

标签: reactjs react-router-v4


【解决方案1】:

SubComponentOne 路由需要有 exact 关键字

            <Switch>
              <Route exact path="/dash/overview/" render={() => <SubComponentOne/>}/>
              <Route path="/dash/subtwo" render={() => <SubComponentTwo/>}/>
            </Switch>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2021-02-08
    • 1970-01-01
    • 2017-05-12
    • 2022-11-14
    • 2018-05-23
    相关资源
    最近更新 更多