简体   繁体   English

React Native:如何使用 DeviceInfo isTablet() 方法有条件地将样式应用于元素?

[英]React Native: How can I use the DeviceInfo isTablet() method to conditionally apply a style to an element?

I am trying to adapt the design of my app to tablet and one way to detect if the app is running on a tablet is by using the DeviceInfo module in particular the isTablet() method.我正在尝试使我的应用程序的设计适应平板电脑,检测应用程序是否在平板电脑上运行的一种方法是使用 DeviceInfo 模块,特别是 isTablet() 方法。 How can I use this method to conditionally apply styles to an element?如何使用此方法有条件地将 styles 应用于元素?

Here is what I am trying to do at the moment:这是我目前正在尝试做的事情:

import { checkIfDeviceIsTablet } from './helper-functions';

<View style={[styles.wrapper, checkIfDeviceIsTablet() === true ? styles.wrapperTablet : {}]}>
    {contents}
</View>

The checkIfDeviceIsTablet() function is as follows: checkIfDeviceIsTablet() function 如下:

import DeviceInfo from 'react-native-device-info';

function checkIfDeviceIsTablet() {

    DeviceInfo.isTablet().then(isTablet => {
        return isTablet;
    });

}

The issue is that when the component loads the checkIfDeviceIsTablet() method returns a promise as opposed to the expected true/false value and so the conditional styles are not applied when the app is run on a tablet.问题是当组件加载时 checkIfDeviceIsTablet() 方法返回 promise 而不是预期的真/假值,因此当应用程序在平板电脑上运行时,条件 styles 不会应用。 I tried turning the function into an async/await format with a try/catch but the result is the same.我尝试使用 try/catch 将 function 转换为 async/await 格式,但结果是一样的。

I would use React Native's own Platform.isPad function but the app must also work on Android.我会使用 React Native 自己的 Platform.isPad function,但该应用程序也必须在 Android 上运行。

Any help is appreciated.任何帮助表示赞赏。

I would recommend calling DeviceInfo.isTablet() only once at the beginning of your app.我建议您在应用开始时只调用一次DeviceInfo.isTablet() You can store the result globally, and then later on you can check the type without having to deal with async promises.您可以全局存储结果,然后您可以检查类型而无需处理异步承诺。

To store the type globally, your options are:要全局存储类型,您的选择是:

  • A global variable全局变量
  • React's Context API React 的上下文 API
  • A static property on a class (if using ES6+) class 上的 static 属性(如果使用 ES6+)
  • Some sort of global state management solution like Redux某种全局 state 管理解决方案,例如 Redux

You still have to deal with the initial async problem, since the first call to DeviceInfo.isTablet() will return an async promise.您仍然必须处理初始异步问题,因为第一次调用DeviceInfo.isTablet()将返回异步 promise。

I'd recommend looking into React's Context API.我建议查看 React 的 Context API。

Here's a rough example:这是一个粗略的例子:

render() {
   return (
      <DeviceInfoContext.Consumer>
      { ({ isTablet }) => (
         <Text>Is this a tablet? {isTablet}</Text>
      ) }
      </DeviceInfoContext.Consumer>
   )
}

And your DeviceInfoContext class would look something like this:您的DeviceInfoContext class 看起来像这样:

class DeviceInfoContext extends React.Component {
   state = {
      isTablet: false
   }

   componentDidMount() {
      Device.IsTablet().then(result => this.setState({ isTablet: result }))
   }

   render() {
      return (
         this.props.children({ isTablet: this.state.isTablet })
      )
   }
}

This is just a rough example.这只是一个粗略的例子。 You can learn more about the Context API in the docs您可以在文档中了解有关上下文 API 的更多信息

Me too had some troubles with the breaking changes of react native 0.5xx to 0.6xx.我也对 react native 0.5xx 到 0.6xx 的破坏性更改遇到了一些麻烦。 The library for device detection change it structure to promises.设备检测库将其结构更改为 Promise。 A paintful.一幅画。

This library save the day, the installation and use is very easy.这个库节省了一天,安装和使用非常容易。 https://github.com/m0ngr31/react-native-device-detection https://github.com/m0ngr31/react-native-device-detection

import { isTablet } from 'react-native-device-detection;

// isTablet is a boolean. Return false o true immediately

//So ...

import styled from 'styled-components/native';
import theme from 'styled-theming';  
import { isTablet } from 'react-native-device-detection';

const CoverPageDateText = styled.Text`
font-size: ${isTablet ? 23 : 17};
color: gray; 
padding-bottom: 9;
`

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

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