为什么我不能在 react-router 4.x 中嵌套 Route 组件? [英] Why can I not nest Route components in react-router 4.x?

查看:23
本文介绍了为什么我不能在 react-router 4.x 中嵌套 Route 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 react-router 中使用嵌套路由,具体来说,版本4.x?以下在以前的版本中运行良好...

<Route path='/stuff/a' component={StuffA}/></路线>

升级到 4.x 会抛出以下警告...

<块引用>

警告:您不应该使用<Route>组件和<路由子级>在同一条路线上;<路由子项>将被忽略

这到底是怎么回事?我已经搜索了文档几个小时,但无法成功使嵌套路由正常工作.如何使用 components 在 react-router v4 中嵌套他们的路由?我的简单示例如何转换为 v4.x API 合规性以嵌套路由?

解决方案

忘记你对 React Router 的了解 <v4.您可以通过逐字嵌套 来嵌套路由.检查这个例子.具体查看主题组件.您不会预先声明路由,而是在组件呈现时动态声明.

从 'react' 导入 React进口 {BrowserRouter 作为路由器,路线,关联来自'反应路由器-dom'const BasicExample = () =>(<路由器><div><ul><li><Link to="/">首页</Link></li><li><Link to="/about">关于</Link></li><li><Link to="/topics">Topics</Link></li><小时/><路由精确路径="/" component={Home}/><Route path="/about" component={About}/><Route path="/topics" 组件={Topics}/>

</路由器>)const Home = () =>(<div><h2>家</h2>

)const 关于 = () =>(<div><h2>关于</h2>

)const 主题 = ({ 匹配 }) =>(<div><h2>主题</h2><ul><li><链接到={`${match.url}/rendering`}>使用 React 渲染</链接><li><链接到={`${match.url}/components`}>组件</链接><li><链接到={`${match.url}/props-v-state`}>道具诉国家</链接>{/* 嵌套路由 */}<路由路径={`${match.url}/:topicId`} component={Topic}/><路由精确路径={match.url} render={() =>(<h3>请选择一个主题.</h3>)}/>

)const 主题 = ({ 匹配 }) =>(<div><h3>{match.params.topicId}</h3>

)导出默认 BasicExample

How in the world does one use nested routes in react-router, specifically, version 4.x? The following worked well in previous versions...

<Route path='/stuff' component={Stuff}>
  <Route path='/stuff/a' component={StuffA} />
</Route>

Upgrading to 4.x throws the following warning...

Warning: You should not use <Route> component and <Route children> in the same route; <Route children> will be ignored

What in the heck is going on here? I've scoured the docs for hours and can not successfully get nested routes working. How does one use <Route>components to nest their routes in react-router v4? How does my simplistic example translate to v4.x API compliance to nest a route?

解决方案

Forget what you know about React Router < v4. You nest routes by literally nesting <Routes>. Check this example. Specifically check out the Topics component. You don't declare your routes up front but instead dynamically when a component renders.

import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

const BasicExample = () => (
  <Router>
    <div>
      <ul>
        <li><Link to="/">Home</Link></li>
        <li><Link to="/about">About</Link></li>
        <li><Link to="/topics">Topics</Link></li>
      </ul>

      <hr/>

      <Route exact path="/" component={Home}/>
      <Route path="/about" component={About}/>
      <Route path="/topics" component={Topics}/>
    </div>
  </Router>
)

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
)

const About = () => (
  <div>
    <h2>About</h2>
  </div>
)

const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>
          Rendering with React
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>
          Components
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>
          Props v. State
        </Link>
      </li>
    </ul>

    {/* NESTED ROUTES */}
    <Route path={`${match.url}/:topicId`} component={Topic}/>
    <Route exact path={match.url} render={() => (
      <h3>Please select a topic.</h3>
    )}/>
  </div>
)

const Topic = ({ match }) => (
  <div>
    <h3>{match.params.topicId}</h3>
  </div>
)

export default BasicExample

这篇关于为什么我不能在 react-router 4.x 中嵌套 Route 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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