React conditionally render based on viewport size

后端 未结 4 2044
太阳男子
太阳男子 2021-01-30 11:24

This should be an easy one for you React monsters. :) I have the condition written but I can\'t figure out how to handle the viewport size in my constructor for the condition to

4条回答
  •  别那么骄傲
    2021-01-30 12:07

    class My Class extends React.Component {
      constructor(props) {
          super(props);
          this.state = {
              isDesktop: window.innerHeight > 1450,
          };
      }
    
      render() {
          const isDesktop = this.state.isDesktop;
    
          return (
              
    {isDesktop ? (
    I show on 1451px or higher
    ) : (
    I show on 1450px or lower
    )}
    ); }}

    Here more info Get viewport/window height in ReactJS

提交回复
热议问题