且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ReactJS中获取视口/窗口高度

更新时间:2023-01-26 11:42:39

class AppComponent extends React.Component {

  constructor(props) {
    super(props);
    this.state = {height: props.height};
  }

  componentWillMount(){
    this.setState({height: window.innerHeight + 'px'});
  }

  render() {
    // render your component...
  }
}




设置道具

Set the props



AppComponent.propTypes = {
 height:React.PropTypes.string
};

AppComponent.defaultProps = {
 height:'500px'
};




视口高度现在可用作{this.state.height} in渲染模板

viewport height is now available as {this.state.height} in rendering template