简体   繁体   English

React:一个组件的多个JSX模板

[英]React: multiple JSX templates for one single component

I am a React newbie and I am developing a React application initially created using create-react-app module and I need it to be adapted to Mobile devices(browser)... For that I am facing the problem of having a Mobile and a Web version of the same component. 我是一个React新手,我正在开发一个最初使用create-react-app模块创建的React应用程序,我需要使其适应移动设备(浏览器)...为此,我面临的问题是拥有移动设备和相同组件的Web版本。 I was wondering if there is a way to optimise the process of two separate components by only having 2 different JSX rendering template and keeping the same code base. 我想知道是否有一种方法可以通过仅具有2个不同的JSX呈现模板并保持相同的代码库来优化两个单独组件的过程。 I mean loading specific JSX depending on device and place it in the render method. 我的意思是根据设备加载特定的JSX并将其放置在render方法中。

Any help will be appreciated Thanks in advance 任何帮助将不胜感激预先感谢

try react-responsive library. 尝试使用响应式库。

you can render the different component based on mediaQuery 您可以基于mediaQuery呈现不同的组件

lib reference 库参考

import MediaQuery from 'react-responsive';

const Example = () => (
  <div>
    <div>Device Test!</div>
    <MediaQuery query="(min-device-width: 1224px)">
      <div>You are a desktop or laptop</div>
      <MediaQuery query="(min-device-width: 1824px)">
        <div>You also have a huge screen</div>
      </MediaQuery>
      <MediaQuery query="(max-width: 1224px)">
        <div>You are sized like a tablet or mobile phone though</div>
      </MediaQuery>
    </MediaQuery>
    <MediaQuery query="(max-device-width: 1224px)">
      <div>You are a tablet or mobile phone</div>
    </MediaQuery>
    <MediaQuery query="(orientation: portrait)">
      <div>You are portrait</div>
    </MediaQuery>
    <MediaQuery query="(orientation: landscape)">
      <div>You are landscape</div>
    </MediaQuery>
    <MediaQuery query="(min-resolution: 2dppx)">
      <div>You are retina</div>
    </MediaQuery>
  </div>
);

i'm using Semantic-ui React and there is a Responsive component that you can render ad specific component and specify the resolution: for example 我正在使用Semantic-ui React,并且有一个Responsive组件,您可以呈现广告特定的组件并指定分辨率:例如

<Responsive as={Grid} {...Responsive.onlyMobile} columns='equal' >
   //stuff for mobile
</Responsive>

<Responsive as={Grid} {...Responsive.onlyTablet} columns='equal' >
   //stuff for tablet
</Responsive>

You can use something like Bootstrap. 您可以使用Bootstrap之类的东西。 If this does not solve the problem. 如果这样不能解决问题。 Then you can have some Flag State and have two functions returning different JSX markup. 然后,您可以具有一些标志状态,并具有两个返回不同JSX标记的函数。 You will have to write logic for detecting Mobile. 您将必须编写用于检测Mobile的逻辑。

Other answer rely on screen size (that can lead to false positives), this solution relies on detecting mobile devices 其他答案取决于屏幕尺寸 (可能导致误报),此解决方案依赖于检测移动设备

You can use react-device-detect 您可以使用react-device-detect

<BrowserView>
    <h1> This is rendered only in browser </h1>
</BrowserView>
<MobileView>
    <h1> This is rendered only on mobile </h1>
</MobileView>

This can wrap a single view or an entire set of routes inside a <Switch /> 这可以在<Switch />内包装单个视图或整个路由集

We can use Rebass library which is used to build the responsive component for mobile, tablet and desktop as well. 我们可以使用Rebass库,该库也用于为移动设备,平板电脑和台式机构建响应组件。 Along with we can use styled component , so that we can easily style our component based on our own CSS. 我们还可以使用样式化的组件 ,以便我们可以轻松地根据自己的CSS对组件进行样式设置。

https://rebassjs.org/ https://rebassjs.org/

https://www.styled-components.com/ https://www.styled-components.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM