我有一个用于 Success 的通用组件,并通过导航路线呈现该组件。
成功组件.jsx
var pageTitle;
const SuccessComponent = (props) => {
pageTitle = props.location.state.title;
return(
<> jsx code here </>
)
}
//This title needs to be dynamic, not getting props here hence took var pageTitle but getting undefined.
let SuccessComp = withTitle({component: SuccessComponent, title: pageTitle})
export default SuccessComp;
WithTitle 组件通过 react-helmet 库设置标题并在每个屏幕上更新。我需要更改 SuccessComponent 不同调用的标题。我怎样才能做到这一点?
我正在使用 SuccessComponent 如下。
我的组件.jsx
export default MyComponent = () => {
const onSubmit = () => {
props.history.push({pathname:'/success',state:{title: 'my comp'})
}
return(
<> jsx code here </>
)
}
MyComponent1.jsx
export default MyComponent1 = () => {
const onSubmit = () => {
props.history.push({pathname:'/success',state:{title: 'my comp 1'})
}
return(
<> jsx code here </>
)
}
withTitle.jsx
export default function withTitle({component: Component, title}){
return function title(props){
(
<>
<Helmet>
<title>{title}</title>
</Helmet>
<Component {...props} />
</>
)
}
}