You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Expo React Native项目中获取当前设备类型

设备类型识别接口getDeviceTypeAsync()返回结果读取方案

getDeviceTypeAsync()属于异步API,需通过异步处理逻辑读取返回结果,两种常用实现方式如下:

  • async/await 调用方式(代码可读性更高,推荐使用)
// 调用逻辑需要放在被async标记的函数内部
async function getCurrentDevice() {
  try {
    const res = await getDeviceTypeAsync()
    // res即为接口返回的设备类型结果
    console.log('设备类型返回值:', res)
    // 可根据返回值做对应业务逻辑,常见返回枚举值包含phone/tablet/desktop/tv等,以实际运行环境的定义为准
    if(res === 'phone') {
      // 手机端适配逻辑
    } else if(res === 'desktop') {
      // 桌面端适配逻辑
    }
  } catch (error) {
    // 捕获接口调用异常
    console.error('设备类型获取失败:', error)
  }
}
  • Promise .then() 调用方式(无需修改外层函数标记,适合零散逻辑调用)
getDeviceTypeAsync()
.then(res => {
  // 此处读取返回的设备类型结果
  console.log('设备类型返回值:', res)
})
.catch(error => {
  console.error('设备类型获取失败:', error)
})

如果不清楚当前环境下的返回值范围,可先在调试环境打印完整返回结果,再对应开发分支逻辑。

内容的提问来源于stack exchange,提问作者Nothing here

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:09:02