Expo环境下React Native View组件渲染尺寸与设定值不符问题求助
Expo 开发 React Native 应用元素渲染尺寸随机偏差问题
问题现象
- 部分渲染元素未按代码指定的宽高值渲染,实际尺寸存在小幅随机偏差,较设定值略大或略小
- 简单列表场景下影响较小,嵌套数组的复杂布局场景下会出现非预期异常间距,可从组件边框渲染效果直观观察到异常
复现条件
- 可在版本3.22.3的全新空白Expo项目中稳定复现
- 示例代码在React Native Web环境下运行完全正常,无法通过CodeSandbox这类网页端环境复现
示例代码
简单布局示例
效果参考:可直观观察到基础尺寸偏差问题
import React from 'react' import { StyleSheet, View } from 'react-native' export default function App() { const array = [1, 1, 1, 1, 1, 1, 1, 1, 1] return ( <View style={styles.container}> {array.map(() => ( <View style={styles.row} key={Math.random()}></View> ))} </View> ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center' }, row: { borderWidth: 0.5, backgroundColor: 'red', width: 30, height: 30 } })
嵌套布局示例
效果参考:复杂嵌套场景下异常间距会被放大
import React from 'react' import { StyleSheet, View } from 'react-native' export default function App() { const array = [ [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1] ] return ( <View style={styles.container}> {array.map((row) => ( <View style={styles.row} key={Math.random()}> {row.map(() => ( <View style={styles.column} key={Math.random()}></View> ))} </View> ))} </View> ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center' }, row: { flexDirection: 'row' }, column: { borderWidth: 0.5, backgroundColor: 'red', width: 33, height: 33 } })
诉求
征求同类问题的遇到经验或对应解决方案。
内容的提问来源于stack exchange,提问作者spitox
相关产品推荐
相关产品推荐

