Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get size of a View before render it for react-native FlatList getItemLayout

I need set initialScrollIndex on FlatList to 1, so second row will be visible on initial loading. It's depend on getItemLayout. My FlatList cells contains text of different length, which leads to different height of each cell, which leads i can not return fixed value in getItemLayout. My strategy is:

  1. On load, iterate through dataset for list and calculate height of each row
  2. Cache calculated data into array or dictionary
  3. Return cached data inside getItemLayout depend on index

The problem is - i can not find the way how to calculate the height of an view before it will be mounted into DOM hierarchy. I read a lot about onLayout prop, but it looks like View should be rendered first to get it to work.

So, what do you use to achieve scrolling to specified row in react-native when list cell is not the same for each row?

like image 310
George Avatar asked Jan 01 '18 14:01

George


1 Answers

My first thought - try render it hidden, take size and make it visible.

EDIT: You also can try use react-native-text-size to calculate cell heights manually.

like image 106
Alexander Danilov Avatar answered Oct 07 '22 23:10

Alexander Danilov