Navigate back to the homepage

React Native ScrollView

Infinitbility
ScrollView
February 10th, 2021 · 1 min read
React Native ScrollView

This Article Part of React Native Tutorial Series want to start from scrach follow below link

https://infinitbility.com/react-native/table-of-contents


Ever have the situation where you sometimes had content that was shorter than the screen size and didn’t require scrolling but occasionally had content taller than the screen size, thus necessitating scroll to allow the user to see all the content.

React native scrollview provide feature to create scrolllable content on both direction ( vertically and horizontally ).

React Native ScrollView example.

App.js

1import React, { Component } from "react";
2import { ScrollView, StyleSheet, Text, View } from "react-native";
3class App extends Component {
4 state = {
5 products: [
6 { product: "Rice", id: 1 },
7 { product: "Sweets", id: 2 },
8 { product: "Fruits", id: 3 },
9 { product: "Animals", id: 4 },
10 { product: "humans", id: 5 },
11 { product: "sport", id: 6 },
12 { product: "kitchen", id: 7 },
13 { product: "childrens", id: 8 },
14 { product: "men", id: 9 },
15 { product: "old people", id: 10 },
16 { product: "shafty", id: 11 },
17 { product: "transport", id: 12 }
18 ]
19 };
20 render() {
21 return (
22 <View>
23 <ScrollView>
24 {this.state.products.map((item, index) => (
25 <View key={item.id} style={styles.item}>
26 <Text>{item.product}</Text>
27 </View>
28 ))}
29 </ScrollView>
30 </View>
31 );
32 }
33}
34const styles = StyleSheet.create({
35 item: {
36 flexDirection: "row",
37 justifyContent: "space-between",
38 alignItems: "left",
39 padding: 20,
40 margin: 2,
41 borderColor: "pink",
42 borderWidth: 3,
43 backgroundColor: "yellow"
44 }
45});
46export default App;

Scroll horizontally

react native scrollview provide horizontal props. When true, the scroll view’s children are arranged horizontally in a row instead of vertically in a column. The default value is false.

Use like below example

1...
2 <ScrollView
3 horizontal={true}
4 >
5 {this.state.products.map((item, index) => (
6 <View key={item.id} style={styles.item}>
7 <Text>{item.product}</Text>
8 </View>
9 ))}
10 </ScrollView>
11...

Common Q & A

Common Q & A section explain commonly question ask on google.

How do you get the scroll position in react native?

react native scrollview provide onScroll props to get scrolled position of screen.

just like below example

1<ScrollView
2 onScroll={event =>
3 this.setState({horizontalScroll:event.nativeEvent.contentOffset.x, VerticalScroll:event.nativeEvent.contentOffset.y})
4 }
5>
6// Your list data
7</ScrollView>

React native Scroll programmatically

infinitbility already published Tutorial related to Scroll programmatically. please check out the below link.

React native ScrollView scroll to position

Thanks For reading…

More From React Native Tutorial

Basics

Advances

Error & Issue Solution

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

More articles from Infinitbility

React Native Textinput

React Native Textinput

React Native Tutorial - React Native Textinput

January 28th, 2021 · 1 min read
React Native Text

React Native Text

React Native Tutorial - React Native Text

January 27th, 2021 · 1 min read
© 2021 Infinitbility
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]