React-Router 4.2 的嵌套路由实现

React-Router的之前版本支持路由通过path和Route标签直接嵌套,看起来简洁明了,但是4之后的版本不支持了,原因据说是为了践行React的组件化理念,不能让Route标签看起来只是一个标签…。

//示例1、RR4之前的嵌套路由写法
import {
    Router,
    Route,
    IndexRoute,
    hashHistory
} from 'react-router';
import App from './views/App.js';
import Login from './views/Login';

class MyMouter extends React.Component {
    render(){
        return(
            <Router history={hashHistory}>
            <Route path="/" component={App}>
            <IndexRoute component={Login}/>
            <Route path="login" component={Login}/>
            <Route path="userInfo" getComponent={pageFrame}>
                        <Route path="change" getComponent={userInfoChange}/>
                    </Route>
                 </Route>
             </Router>  
        )
    }
}
export default MyRouter

既然不能使用Route嵌套,那该怎么办呢?由两种写法:

  • 在component组件内部需要嵌套的位置直接嵌套Route标签
    如示例1、RR4之前的路由嵌套写法,在component组件中需要使用this.props.children来表示嵌套子组件,而在4中,我们必须直接将Route标签写入到父组件之中,而且路由必须包含根路径,RR4不会帮我们自动拼接location。
//示例2、Route在componnt中的直接嵌套
//root.js
    ReactDOM.render(
        <Provider store={store}>
            <ConnectedRouter history={hashHistory}>
                <div>
                    <Route path="/" component={App} />
                </div>
            </ConnectedRouter>
        </Provider>,
        document.getElementById('root')
    )
//App.js
  render() {
    return (
      <div className="App">
        <Switch>
            <Route path="/login" exact render={(({location}) => (<h1>来玩啊,小帅哥</h1>)) } />
            <Route path="/" component={Portal} />
        </Switch>
      </div>
    );
  }

//Portal.js
  render() {
    return (
<Content style={{background : '#FFF', padding : 24, margin : 0, minHeight : 200}}>
    <Route path="/" exact render={() => (<h1>弗雷尔卓德必将再次统一!</h1>)} />
    <Route path="/products" exact component={Products} />
    <Route path="/search" exact component={ProductSearch} />
</Content>
    );
  }
  • 使用Route render渲染作内联嵌套

使用Route component方法进行路由嵌套遵从了组件化要求,但也使Route标签分散在各个层级的组件之中,如果我们仍然喜欢使用4之前的集中配置方法,可以使用Route的render方法进行内联嵌套。此时,component仍可以使用this.props.children进行嵌套渲染。

//示例3、render内联渲染
ReactDOM.render(
    <Provider store={store}>
        <ConnectedRouter history={hashHistory}>
            <div>
                <Route path="/" render={({history,location}) => (
                    <App history={history} location={location} />
                        <Switch>
                            <Route path="/login" exact render={(({location}) => (<h1>来玩啊,小帅哥</h1>)) } />
                            <Route path="/" render={({history,location,match}) => (
                                <Portal history={history} location={location} match={location}>
                                    <Route path="/" exact render={() => (<h1>弗雷尔卓德必将再次统一!</h1>)} />
                                    <Route path="/products" exact component={Products} />
                                    <Route path="/search" exact component={ProductSearch} />
                                </Portal>
                                )} />
                        </Switch>
                    </App>
                )} />
            </div>
        </ConnectedRouter>
    </Provider>,
    document.getElementById('root')
)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在React Router v6嵌套路由的使用方式略有不同。你可以使用`<Route>`组件来定义嵌套路由,并使用`element`属性指定要渲染的组件。下面是一个示例: ```jsx import { Route, Routes } from 'react-router-dom'; import { News } from './News'; import { Message } from './Message'; export const Home = () => { return ( <div> <h3>Home</h3> <div> <ul className="nav nav-tabs"> <li> <MyNavLink to="news">News</MyNavLink> </li> <li> <MyNavLink to="message">Message</MyNavLink> </li> </ul> {/* 注册路由 */} <Routes> <Route path="news" element={<News />} /> <Route path="message" element={<Message />} /> </Routes> </div> </div> ); }; ``` 在上面的代码,我们使用`<Routes>`组件包裹了多个`<Route>`组件,每个`<Route>`组件定义了一个嵌套路由的路径和要渲染的组件。例如,当路径为"/news"时,会渲染`<News>`组件。这样,当用户访问"/news"时,`<News>`组件就会被渲染在嵌套路由的位置上。 希望这能回答你的问题!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [React Router v6 路由配置,嵌套路由](https://blog.csdn.net/Snow_GX/article/details/123656412)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [react-router-dom v6版本 嵌套路由](https://blog.csdn.net/jzh1359314792/article/details/126526047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值