简体   繁体   English

React Native FlexBox & Wrap - 如何检测元素是否移动到下一行?

[英]React Native FlexBox & Wrap - how to detect if element moved to next row?

I'm having a React Native View with flexDirection: 'row' and flexWrap: 'wrap' .我有一个带有flexDirection: 'row'flexWrap: 'wrap'的 React Native View Inside I render a bunch of tags with icons, some of them move to next row.在里面我渲染了一堆带有图标的标签,其中一些移到了下一行。

I would like to detect new row to not render the dot marked with yellow:我想检测新行以不呈现标有黄色的点:

在此处输入图像描述

Any idea how to achieve that?知道如何实现吗?

You can be able to know the initial height of the flexbox, and then attach an onLayout event to it so when the height exceeds your initial height you will know that the items have now span another row down due to the flex-wrap, this is the syntax for the onLayoutEvent您可以知道 flexbox 的初始高度,然后为其附加一个 onLayout 事件,这样当高度超过您的初始高度时,您将知道由于 flex-wrap,这些项目现在已经向下跨越了另一行,这是onLayoutEvent 的语法

 <View onLayout={(event) => {
  let {height} = event.nativeEvent.layout;
   }} />

Depending on the height, you can update your component state and base on that to conditionally apply a style to your UI elements.根据高度,您可以更新组件 state 并基于它有条件地将样式应用于您的 UI 元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM