Javascript 为什么设置状态不触发重新渲染?

Javascript 为什么设置状态不触发重新渲染?,javascript,reactjs,routing,Javascript,Reactjs,Routing,我正在尝试触发重新渲染以排除页眉和页脚 当我记录页面时,它首先触发render>componentDidMount>render,因此我假设页面确实使用正确的值进行了更新,但它仍然显示页眉/页脚 constructor() { super(); this.state = { header: true, footer: true, }; } componentDidMount() { if (window.location.pathn

我正在尝试触发重新渲染以排除页眉和页脚

当我记录页面时,它首先触发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}
 }
constructor(){
超级();
此.state={
标题:对,
页脚:是的,
};
}
componentDidMount(){
如果(window.location.pathname=='/404页'){
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(){
{头?:空}
}

结果将是页眉/页脚不再存在。

首先必须从
状态
对属性
页眉
进行分解。然后明确地
返回

 render() {
    const {header} = this.state
    return header ? <Header /> : null
 }
render(){
const{header}=this.state
返回头?:空
}

首先,您必须从
状态
对属性
标题
进行解构。然后明确地
返回

 render() {
    const {header} = this.state
    return header ? <Header /> : null
 }
render(){
const{header}=this.state
返回头?:空
}

哇,真不敢相信我错过了。。但它仍然无法修复编辑:忘记返回:)干杯!它是固定的!你检查了标题值了吗?哇,真不敢相信我居然漏掉了。。但它仍然无法修复编辑:忘记返回:)干杯!它是固定的!你检查了标题值了吗?