在 React 中使用 componentDidMount 和 componentWillUnmount 切换进出?

时间:2021-03-19 21:39:36

标签: reactjs toggle react-lifecycle

“SideBar”组件在页面加载时按预期切换,但是...我不知道如何在加载另一个页面时将其切换回。我是 React 的新手,任何帮助将不胜感激。 提前致谢。

constructor(props) {
  super(props);
  
  this.state = {
    headlines:[],
    isLoaded: false,
    error:null
  }
}

componentDidMount(){
  this.toggleSideBar();
}

componentWillUnmount(){
    this.toggleSideBar();
}

toggleSideBar = event => {
    this.sidebar.toggle()
}

render() {
    return (

        <div>
            <Row className="d-none d-md-block">
                <SideBar style={{ backgroundColor: 'rgba(0, 0, 0, 0.800)'}}
                         ref={(sidebar => this.sidebar = sidebar)}>
                    <div style={{marginTop: "16vh"}}><TFGuage/></div>
                    <div style={{marginTop: "0vh"}}><GBGuage/></div>
                </SideBar>
            </Row>
        </div>
    );
}

}

0 个答案:

没有答案