react-native - Flex 在 facebook.io 上的工作方式与在实际应用程序 React Native 中的工作方式不同

标签 react-native

我试图使 ScrollView 子项的大小与 ScrollView 本身的大小相同。代码非常简单:

  <View style={{ flex: 1 }}>
    <ScrollView style={[{ flex: 1, backgroundColor: 'green' }]}>
      <View style={{ flex: 1, backgroundColor: 'red' }}>
      </View>
    </ScrollView>
  </View>

问题是,在模拟器本地,我得到了绿屏( subview 甚至没有出现在元素检查器中)。

关于facebook tutorials结果不同:

enter image description here

为什么它的工作方式不同?

最佳答案

您遇到的差异实际上是 React Native Web 之间的差异,官方 React Native 文档在其示例中使用它,以及 React Native。

鉴于 React Native Web 并不直接依赖 React Native 作为依赖项,这种不一致很可能是由于 <ScrollView> 的不同实现造成的。组件以及如何处理和/或传播样式属性。

一般来说,如果官方文档中提供的示例也有显示结果的设备,我不会理所当然地认为它们,因为这些示例很可能是为在 React Native Web 上工作而定制的,这可能会也可能不会(就像在本例中一样)表现得像 React Native。

详细信息

React Native 的 <ScrollView>实际上将 subview 包装成 <ScrollContentContainerViewClass> ,因此需要区分 ScrollView 本身的样式(通常 style 属性)和此包装器的样式(@yangguang1029's answer 中提到的 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>
);

来源:React Native ScrollView source on GitHub

关于react-native - Flex 在 facebook.io 上的工作方式与在实际应用程序 React Native 中的工作方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736774/

相关文章:

javascript - React-Native 在按下通知后导航

javascript - 如何为所有选项卡设置相同的背景图片? ( react native )

javascript - react native : Error while using built-in button

ios - React Native iOS 8.1 应用程序在访问新 View 时崩溃 - 仅在不使用调试器时

javascript - AWS 放大 : onStatusChange then render main page

dependencies - "Unable to resolve module"在 native react

module - ReactNative - 未定义不是一个对象(评估 x)

node.js - 如何在 React Native 中导入 Node 模块

react-native - 如何在我的 React Native 项目中获取值

android - 仅学习 React Native 是否足以构建 iOS 和 Android 应用程序?