[英]Why does the setState not trigger a re-render?
I'am trying to trigger a re-render to exclude my header and footer. 我正在尝试触发重新渲染以排除页眉和页脚。
When I log the page, it first triggers render > componentDidMount > render, so I would assume the page did update with the correct values, but it still shows the header/footer. 当我登录页面时,它首先触发render> componentDidMount> render,因此我认为页面确实使用正确的值进行了更新,但仍显示页眉/页脚。
constructor() {
super();
this.state = {
header: true,
footer: true,
};
}
componentDidMount() {
if (window.location.pathname === '/404-page') {
this.setState({ header: false, footer: false });
} else if (window.location.pathname === '/form') {
this.setState({ header: true, footer: false });
} else if (window.location.pathname.length > 6) {
this.setState({ header: true, footer: false });
}
}
render() {
{header ? <Header /> : null}
}
The result would be that the header/footer aren't there anymore. 结果将是页眉/页脚不再存在。
First you have to destructure the property header
from state
. 首先,您必须从
state
解构属性header
。 Then explictly return
然后明确
return
render() {
const {header} = this.state
return header ? <Header /> : null
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.