【发布时间】:2018-10-03 12:17:41
【问题描述】:
我发现自己在使用 react 模板和路由。 我无法将道具传递给内部组件以更改页面标题。
路由在模板中定义如下,我为每个路由添加了“title”道具,以便传递给内部组件。
const loadable = loader =>
Loadable({
loader,
delay: false,
loading: () => null,
})
const loadableRoutes = {
'/registration': {
component: loadable(() => import('sm-pages/RegistrationPage')),
title : "Registration"
},
'/registrationSuccess': {
component: loadable(() => import('sm-pages/RegistrationPage')),
title : "Registration Success"
},
...
render() {
return (
<ConnectedSwitch>
<Route exact path="/" component={HomePage} />
{Object.keys(loadableRoutes).map(path => {
const { exact, ...props } = loadableRoutes[path]
props.exact = exact === void 0 || exact || false // set true as default
return <Route key={path} path={path} {...props} />
})}
<Route
render={() => (
<Page>
<NotFoundPage />
</Page>
)}
/>
</ConnectedSwitch>
)
}
模板有不同的内部组件,有时它会呈现我的组件如下:
render() {
const { getContentBuffer } = this.context
const { pathName, content } = getContentBuffer()
return isEmpty(content) ? (
<div className="utils__loadingPage" />
) : (
<div className="utils__content">
<Breadcrumb name={pathName} />
{content}
</div>
)
}
我以这种方式访问组件中的道具(没有成功):
render() {
const props = this.props
return (
<h1>{this.props.title}</h1>
)
}
我必须如何更改才能访问标题道具?
提前致谢
【问题讨论】:
-
什么是
Loadable?到底能不能传道具?
标签: reactjs react-router react-props