admin管理员组文章数量:1404554
I am looking for a solution that will remove a <header/>
and <footer/>
ponent from my signup.js
and signin.js
pages.
Currently, my root index.js
file is shown as
class Template extends React.Component {
render() {
const { children } = this.props
return (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}}
Template.propTypes = {
children: PropTypes.func
}
export default Template
Which is the main layout for all my page, posts, products, etc. Yet without creating another layout, I would like to conditionally remove the <header/>
and <footer/>
ponents from being a part of pages signup.js
and signin.js
As suggested by GatsbyJS I have tried - of which is removing the ponents from all pages.
if (this.props.location.pathname !== "/signup/") {
return (
<main>
{children()}
</main>
)
} else {
return this (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}
I am looking for a solution that will remove a <header/>
and <footer/>
ponent from my signup.js
and signin.js
pages.
Currently, my root index.js
file is shown as
class Template extends React.Component {
render() {
const { children } = this.props
return (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}}
Template.propTypes = {
children: PropTypes.func
}
export default Template
Which is the main layout for all my page, posts, products, etc. Yet without creating another layout, I would like to conditionally remove the <header/>
and <footer/>
ponents from being a part of pages signup.js
and signin.js
As suggested by GatsbyJS I have tried - of which is removing the ponents from all pages.
if (this.props.location.pathname !== "/signup/") {
return (
<main>
{children()}
</main>
)
} else {
return this (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}
Share
Improve this question
asked Apr 3, 2018 at 1:54
DarrenDarren
2,29010 gold badges48 silver badges104 bronze badges
7
- 1 why not create a new template for signin/signup? – muZk Commented Apr 3, 2018 at 1:56
- Haha... haven't got that far in my React/Gatsby learning journey. Does sound like a better idea though. – Darren Commented Apr 3, 2018 at 1:58
- Is there performance reasons why I would want to do that opposed to adding conditions? – Darren Commented Apr 3, 2018 at 1:59
- I think that performance is not an issue with adding conditions. But the main difference is about maintainability. – muZk Commented Apr 3, 2018 at 2:02
- i was thinking the same thing about making different layouts – Omar Commented Apr 3, 2018 at 2:02
2 Answers
Reset to default 4I would use a different template for your signin and signup ponents, but if you don't do that:
You have a typo in your code, in your else
you are returning this(...)
it should return (...)
. This way:
if (this.props.location.pathname !== "/signup/") {
return (
<main>
{children()}
</main>
)
} else {
return (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}
Also, perhaps your if
condition is inverted... because in /signup/
you don't want Header
and Footer
:
if (this.props.location.pathname === "/signup/" || this.props.location.pathname === "/signin/") {
return (
<main>
{children()}
</main>
)
} else {
return (
<main>
<Header/>
{children()}
<Footer/>
</main>
)
}
Alternatively, if you don't want to duplicate code...
const isSignIn = ["/signup/", "/signin/"].indexOf( this.props.location.pathname) !== 0;
return (
<main>
{ !isSignIn && (<Header/>) }
{children()}
{ !isSignIn && (<Footer/>) }
</main>
)
本文标签: javascriptReactJS remove root component on certain pageStack Overflow
版权声明:本文标题:javascript - ReactJS remove root component on certain page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744839276a2627821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论