Creating Different mobile layout for a component in ReactJs

故事扮演 提交于 2019-12-21 21:16:13

问题


How can we create a completely different (mobile type layout) for a component (having a different desktop type layout) using ReactJs. (Not Responsive , responsive is something css has to take care of.) It should be different layout for the component i.e here Creating a page with a menu(header menu) for desktop screens which becomes a navigation sidebar with logo on small screen.


回答1:


Honestly, a simple resposive css layout may be the best solution, but the steps are to

1) Detect in JS if user in on mobile or desktop. For example this question has a good suggestion as an answer: Detecting a mobile browser

2) Use it to decide in your root component that which layout to use:

function isMobile() {
  // some js way to detect if user is on a mobile device
}

class Root extends Component {
  render() {
    return isMobile() ? ( <MobileLayout /> ) : ( <DesktopLayout /> )
  }
}



回答2:


Checkout react-responsive, You can use media query to render different component depending on device size.



来源:https://stackoverflow.com/questions/41478875/creating-different-mobile-layout-for-a-component-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!