react-native - Flex 在 facebook.io 和实际应用程序中的工作方式不同,React Native
问题描述
我试图让ScrollView
孩子的大小与 scrollView 本身一样。代码非常简单:
<View style={{ flex: 1 }}>
<ScrollView style={[{ flex: 1, backgroundColor: 'green' }]}>
<View style={{ flex: 1, backgroundColor: 'red' }}>
</View>
</ScrollView>
</View>
问题是在模拟器上本地我得到绿屏(元素检查器中甚至没有显示子视图)。
在facebook 教程上,结果是不同的:
为什么它的工作方式不同?
解决方案
您遇到的差异实际上是React Native Web(官方 React Native 文档在其示例中使用)与 React Native 之间的差异。
鉴于 React Native Web 不直接依赖 React Native 作为依赖项这一事实,这种不一致很可能是由于<ScrollView>
组件的不同实现以及样式道具的处理和/或传播方式。
一般来说,如果官方文档中提供的示例也有显示结果的设备,我不会认为它们是理所当然的,因为这些示例很可能是为在 React Native Web 上工作而定制的,可能会也可能不会(就像在这个case) 表现得像 React Native。
细节
React Native<ScrollView>
实际上将子视图包装成 a <ScrollContentContainerViewClass>
,因此需要区分滚动视图本身的样式(通常的style
道具)和此包装器的样式(@yangguang1029 的答案contentContainerStyle
中提到的:
// in render function of ScrollView class.
const contentContainer = (
<ScrollContentContainerViewClass
style={contentContainerStyle}> // styling for inner container view.
{children}
</ScrollContentContainerViewClass>
);
// other operations...
return (
<ScrollViewClass {...props} ref={this._setScrollViewRef}> // here the "style" prop is propagated.
{contentContainer}
</ScrollViewClass>
);
推荐阅读
- openshift - OKD 4.5 单节点安装
- javascript - 带有查询参数的 Firebase 函数 GET 请求返回 400 错误
- reactjs - 使用 react-router-dom 匹配 url 路径时如何防止 React 组件卸载
- google-apps-script - 使用电子表格的工作表名称填充行
- python - 多维张量上的感知器
- javascript - 启用远程调试时禁用导航超时
- mysql - 无法将我的 symfony 5 应用程序与 mysql 连接:postgresql 是强制的
- javascript - 通过浏览器访问服务器上的 html 文件
- shell - 如何使用 jq 获取每个值的 json 数组的输出
- javascript - Javascript中img onload-event时没有页面呈现