【发布时间】:2022-01-23 08:32:02
【问题描述】:
我正在尝试将 MainSection 组件用于两个不同的目的(单个故事和所有故事)。为了实现这一点,我想在路由中传递一个属性 home,该属性用于该组件的渲染。 Home 是真还是假,我想根据该布尔值呈现 MainSection 组件。但是,当 MainSection 被渲染时,我的家一直未定义。链接和路由正在更新 URL,但没有使用我想要的道具进行渲染。我做错了吗?
这是我的路线:
function Routes(){
return(
<Switch>
<Route path="/" element={<MainSection home={true} />} />
<Route path="/story" element={ <MainSection home={false} />} />
</Switch>
)
}
这是我的 MainSection 组件的代码:
function MainSection({ home }){
console.log(home)
return(
<div className="main-section">
<BigPicture home={ home }/>
<Quotes home={ home }/>
</div>
)
}
控制台日志不断返回未定义。
谢谢!
【问题讨论】:
标签: reactjs react-router react-router-dom