【问题标题】:Children overflow when using space-between inside container with flexWrap (React-Native)使用带有 flexWrap 的容器内部空间(React-Native)时,儿童溢出
【发布时间】:2021-10-27 20:33:59
【问题描述】:

刚刚注意到在 React-Native 上使用 flexWrap 时出现了一个非常奇怪的行为,并且希望得到一些帮助以了解是否有任何解释。

总结:

我有一个使用 flexWrap 的水平容器,有两个垂直的子容器,每个容器里面有两个正方形。正方形有不同的高度。如果我在水平容器中使用 flexWrap 在垂直子容器中使用 space-between,则子容器会溢出。如果我删除 flexWrap,一切正常。

这个问题不会发生在 react-native-web 上,但会发生在 Android 和 iOS 上。

博览会代码:https://snack.expo.dev/fHKKvLlew

  • 没有flexWrap(预期行为,正常工作)
    <View style={{ backgroundColor: 'purple', flexDirection: 'row' }}>
      <View style={{ borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'red', height: 100, width: 100 }} />
        <View style={{ backgroundColor: 'green', height: 100, width: 100 }} />
      </View>
      <View style={{ borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'blue', height: 150, width: 100 }} />
        <View style={{ backgroundColor: 'yellow', height: 150, width: 100 }} />
      </View>
    </View>

  • 使用flexWrap(意外行为)
    <View style={{ backgroundColor: 'purple', flexDirection: 'row', flexWrap: 'wrap' }}>
      <View style={{ borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'red', height: 100, width: 100 }} />
        <View style={{ backgroundColor: 'green', height: 100, width: 100 }} />
      </View>
      <View style={{ borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'blue', height: 150, width: 100 }} />
        <View style={{ backgroundColor: 'yellow', height: 150, width: 100 }} />
      </View>
    </View>

鉴于仍有大量可用空间,任何人都知道为什么使用 flexWrap 时结果不一样?为什么孩子会溢出?

似乎是一个错误(特别是因为它在网络上运行良好)。如果是这样,是否有任何可能导致类似结果的解决方法的想法?

【问题讨论】:

    标签: react-native flexbox


    【解决方案1】:

    我使用 codesandbox 测试代码,但无法重现问题:

    也许是其他时候导致了问题。

    编辑:

    如前所述,这种行为仅发生在 react native 移动端,而不适用于 react native web,解决此问题的一种方法是添加:

    alignItems: 'flex-start'
    

    正如this 帖子中所述:

    由于性能调整 flexWrap: wrap 不再与 alignItems: 'stretch' (默认值)一起使用。如果您使用 flexWrap: wrap,您可能还需要添加 alignItems: 'flex-start' 样式。

    所以要复制您的配置,您可以尝试:

    <View style={{ alignItems: "flex-start", backgroundColor: 'purple', flexDirection: 'row', flexWrap: 'wrap' }}>
      <View style={{ alignSelf: "stretch", borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'red', height: 100, width: 100 }} />
        <View style={{ backgroundColor: 'green', height: 100, width: 100 }} />
      </View>
      <View style={{ borderWidth: 1, borderColor: 'white', justifyContent: 'space-between' }}>
        <View style={{ backgroundColor: 'blue', height: 150, width: 100 }} />
        <View style={{ backgroundColor: 'yellow', height: 150, width: 100 }} />
      </View>
    </View>
    

    【讨论】:

    • 我认为代码沙箱默认使用 react-native-web。该问题可能仅发生在本机上。得到了一个博览会链接,它确实可以在网络上运行,但不能在 ios 上运行:snack.expo.dev/fHKKvLlew
    • 是的,我在android上测试过,我可以确认有不同的行为。
    • 感谢您的帮助,马特奥。但问题是我需要在父级(默认)中进行拉伸,以便子级受到间隔的影响。因此,在我的情况下,父母中的 flex-start 将无济于事。但是很高兴你找到了原始的东西。天哪,它从 0.28 开始就存在了!
    • 不幸的是,如果你使用 flexWrap,你应该更改默认的 alignItems: 'streach' 也许你可以给我一些关于你想要做什么的更多信息。这样,我可以尝试提供帮助。
    • 正是如此,但我在里面有一些其他组件,而不是视图。你的回答很酷,对齐自我,我会试试的。如果这不起作用,幸运的是我还可以设置一些固定高度来缓解。谢谢!
    猜你喜欢
    • 2017-04-16
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多