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!
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.
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.
Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView instead. Never add a RecyclerView or ListView to a scroll view.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With