首页 > 解决方案 > React Native (Expo) - useWindowDimensions().width 未在方向更改时更新

问题描述

当设备方向改变时,我被基于屏幕尺寸的元素尺寸所困扰。这是我的代码

import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, useWindowDimensions } from 'react-native';

const CleanSearchBar = (props) => {
   const windowWidth = useWindowDimensions().width;

   return (
   <View>
      <TouchableOpacity style={{left: (windowWidth - 127)}}>
         <Text>text</Text>
      </TouchableOpacity>
   </View>);
}

它始终保持初始值。对此有什么帮助吗?谢谢

标签: react-nativeexporesponsivedevice-orientation

解决方案


推荐阅读