Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing onLayout on React Native view

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>

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.

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

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.

like image 328
CAJE Avatar asked Aug 10 '18 12:08

CAJE


People also ask

How do you focus on a view in React Native?

Currently, React-Native doesn't provide a way, to out-of-the-box, traverse the view tree and get the first focusable element to set the focus for it. So you need to use AccessibilityInfo. setAccessibilityFocus passing a reactTag case by case.

How do you overlap view in React Native?

You have have to use position:'absolute' and put the circle element as the last element of the elements list so it comes to top (no need to use zIndex). also the container div must have styles for child elements to be centered.

What is onLayout in React Native?

react-native-on-layout is an npm package that you can include in your app using npm i react-native-on-layout or with yarn add react-native-on-layout . It is a View component that will do this onLayout dance for you and then passes the layout values as parameters to a render prop.

Can we use document getElementById in React Native?

The equivalent of the document. getElementById() method in React is using refs. To select an element, set the ref prop on it to the return value of calling the useRef() hook and access the dom element using the current property on the ref , e.g. ref. current .


1 Answers

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

like image 81
Tom Slutsky Avatar answered Oct 13 '22 14:10

Tom Slutsky