How to detect if screen size has changed to mobile in React?

前端 未结 5 1574
南方客
南方客 2021-01-30 17:25

I am developing a web app with React and need to detect when the screen size has entered the mobile break-point in order to change the state. Specifically I need my sidenav to b

5条回答
  •  北海茫月
    2021-01-30 18:01

    hey I just published a npm package for this issue. Check it out https://www.npmjs.com/package/react-getscreen

    import React, { Component } from 'react';
    import {withGetScreen} from 'react-getscreen'
    
    class Test extends Component {
      render() {
        if (this.props.isMobile()) return 
    Mobile
    ; if (this.props.isTablet()) return
    Tablet
    ; return
    Desktop
    ; } } export default withGetScreen(Test); //or you may set your own breakpoints by providing an options object const options = {mobileLimit: 500, tabletLimit: 800} export default withGetScreen(Test, options);

提交回复
热议问题