Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView minHeight to take up all the space

I have a ScrollView which content doesn't necessarily exceeds it's size. Let's say it's a form where, upon errors, it expands to add error messages and then it exceeds the ScrollView size and thus is why I'm using that scroll.

The thing is that I want the content to always be at least of the same size of the ScrollView. I'm aware of the minHeight property, but the only way I found to use it is re-rendering, which I'd like to avoid.

I'm using redux for state management and this is what I have

<ScrollView
  contentContainerStyle={[ownStyles.container, {minHeight: this.props.scrollHeight}]}
  style={this.props.style}
  showsVerticalScrollIndicator={false}
  onLayout={(event) => {
    scrollHeight = event.nativeEvent.layout.height;
    this.props.setScrollContentHeight(scrollHeight);
  }}
>

Where the this.props.setScrollContentHeight triggers an action which enters the height on the state, and this.props.scrollHeight is said height. So that after the first render where the onLayout function is triggered, the state is updated with the ScrollView's height, which I then assign as minHeight to its content.

If I set the content's style to flex: 1 then the ScrollView won't scroll.

Can you think of a way to avoid that second render for performance purposes?

Thank you!

like image 624
Felipe92 Avatar asked Aug 12 '16 15:08

Felipe92


People also ask

How do I limit ScrollView in React Native?

Use extrapolate clamp to do this. From the docs: 'By default, it will extrapolate the curve beyond the ranges given, but you can also have it clamp the output value. ' By clamping your output value you will stop your animation once it hits the last output value.

When would you use ScrollView over FlatList?

So it is not preferred to use ScrollView if you have a hundred or a thousand items to be displayed on the screen. It is used when you have fewer data items on the list of a limited size. Flatlist: The FlatList Component is an inbuilt react-native component that displays similarly structured data in a scrollable list.

Is ScrollView scrollable?

Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead. Never add a RecyclerView or ListView to a scroll view.

Does FlatList need ScrollView?

You don't need to use extra components like ScrollView to make list data scrollable. The FlatList component also comes with the following features automatically: Header section.


2 Answers

You should use flexGrow: 1 for the ScrollView and flex: 1 inside the ScrollView, to get a ScrollAble but at least as big as possible <View>.

There has been a discussion about it in react-native and tushar-singh had the great idea to it.

like image 170
Marian Rick Avatar answered Sep 28 '22 18:09

Marian Rick


It has been a long time but I struggled with the same issue. The easy way to do it is to use flexGrow: 1 instead of flex:1 on both scrollView's style and contentContainerStyle props. The content will take at least 100% of space and more if needed.

like image 32
Robin Biondi Avatar answered Sep 28 '22 17:09

Robin Biondi