在 React Native 视图上强制 onLayout [英] Forcing onLayout on React Native view

查看:30
本文介绍了在 React Native 视图上强制 onLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 Text 字段的 React Native View,我正在使用 onLayout 道具来做一些依赖于它提供的数据的位置计算.

I have a React Native View containing a Text field, and I'm using the onLayout prop to do some positional calculations which rely on the data it provides.

<View onLayout={this.calculateDimensions}>
  <Text>{this.props.content}</Text>
</View>

这很有效,但有一种情况,content 属性更新为具有相同字符大小的不同字符串.这导致布局不会改变并且 onLayout 不会触发.

This works well, but there is a scenario where the content prop updates to a different string with the same character size. This results in the layout not changing and onLayout not triggering.

每次 content 属性更新时都必须进行这些位置计算.

These positional calculations must occur each time the content prop updates.

注意:我知道有很多方法可以更新组件.更新与布局不同,遗憾的是不会触发 onLayout.

Note: I am aware there are numerous ways to make the component update. Updating is not the same as laying out and sadly does not trigger onLayout.

推荐答案

您可以拥有基于内容的唯一键.每当组件的关键属性发生变化时,它都会强制重新渲染.

You can have a unique key based on the content. whenever a key attribute of a component changes it is forcing a re-render.

这篇关于在 React Native 视图上强制 onLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆