我想切换Detail
和Blog
,但仍然保持Sidebar
。我在下面使用像这样的代码,但它不起作用。你能帮我改正吗?非常感谢。
const routes = (
<Switch component={App}>
<Route exact key="detailpost" path={CelestialSettings.path + 'posts/:slug'}
components={{main: Detail, sidebar: Popular, mainbottom: Categories}} />) }
/>
<Route exact key="listpost" path={CelestialSettings.path}
components={{feature:Featurex, main: Blog, sidebar: Popular, mainbottom: Categories}} />) }
/>
</Switch>
);
class App extends React.Component {
render () {
const {feature, main, sidebar, mainbottom } = this.props;
return (
<div>
<div className="container">
<div className="main-feature" id="main-feature">
{feature}
</div>
</div>
<div id="content" className="site-content">
<div className="container">
<div className="divcontainer">
<div id="po-homepage" className="content-area">
{main}
</div>
<aside id="secondary" className="sidebar widget-area">
<div className="popular_post" id="popular_post">
{sidebar}
</div>
</aside>
</div>
</div>
</div>
<div className="cat-list">
<div className="container">
<div className="thecategories" id="thecategories">
{mainbottom}
</div>
</div>
</div>
</div>
)
}
}
ReactDOM.render(
(routes),
document.getElementById('app')
);
答案 0 :(得分:2)
要在多个路由中保持相同的补充工具栏,可以将补充工具栏组件与交换机组件置于同一级别。粗略的例子:
const routes = (
<div>
<YourSidebar>
{/* ... */}
</YourSidebar>
<Switch component={App}>
<Route ... />
<Route ... />
{/* ... */}
</Switch>
</div>
);
这样,React-Router不会重新渲染(或更新)侧边栏组件,而仅控制<Switch>
的内容。