嵌套路由未显示正确的组件

时间:2020-09-03 11:44:17

标签: reactjs react-router nested-routes

我试图将ShopPage组件中的CollectionPage组件显示为一个嵌套路由,该路由接收collectionId作为参数,但我得到一个空页面。 这些是我的组件。

商店页面:

import React from 'react';
import CollectionsOverview from '../../components/collections-overview/collections-overview.component';
import { Route } from 'react-router-dom'
import CollectionPage from '../collection/collection.component';

const ShopPage = ({ match }) => (
    <div className='shop-page'>
        < Route exact path={`${match.path}`} component={CollectionsOverview} />
        <Route exact path={`${match.path}/collectionId`} component={CollectionPage} />
    </div>
)

export default ShopPage;

CollectionPage组件:

import React from 'react';
import CollectionItem from '../../components/collection-item/collection-item.component';
import './collection.styles.scss';


const CollectionPage=({match})=>{
    console.log(match);
    return(
    <div className="collection-page">
        <h2>Collection Page</h2>
    </div>
)}

export default CollectionPage;

1 个答案:

答案 0 :(得分:2)

似乎collectionId是一个应以:为前缀的参数,例如:

 <Route exact path={`${match.path}/:collectionId`} component={CollectionPage} />

并从App.tsx中的exact中删除<Route path="/shop" component={ShopPage} />,检查this