Lesson 15 of 60 – ScrollView Component in React Native
25%

ScrollView Component in React Native

The ScrollView component is used when content is larger than the available screen area and the user needs to scroll through it. It is useful for forms, long pages, settings screens, descriptions and other content that does not fit on the screen at once.

Note: A ScrollView can contain multiple child components and allows the user to scroll through them. For very large lists of repeated data, components such as FlatList are generally more suitable.

1. What is ScrollView?

ScrollView is a React Native component that provides a scrollable area for its content.

import { ScrollView } from "react-native";

It can be used for both vertical and horizontal scrolling.

2. Importing ScrollView

Import ScrollView from the react-native package.

import { ScrollView } from "react-native";

You can also import it together with other components.

import {
    View,
    Text,
    ScrollView
} from "react-native";

3. Basic ScrollView Syntax

<ScrollView>

    <Text>
        Welcome to React Native
    </Text>

</ScrollView>

All content placed inside the ScrollView becomes part of the scrollable area.

4. ScrollView with Multiple Text Components

<ScrollView>

    <Text>Chapter 1</Text>

    <Text>Chapter 2</Text>

    <Text>Chapter 3</Text>

    <Text>Chapter 4</Text>

    <Text>Chapter 5</Text>

</ScrollView>

The user can scroll through the content when it exceeds the available screen space.

5. Vertical Scrolling

Vertical scrolling is the default behavior of ScrollView.

<ScrollView>

    <Text>Item 1</Text>
    <Text>Item 2</Text>
    <Text>Item 3</Text>
    <Text>Item 4</Text>
    <Text>Item 5</Text>

</ScrollView>

The user can move up and down through the content.

6. Horizontal Scrolling

Horizontal scrolling can be enabled using the horizontal prop.

<ScrollView horizontal>

    <View>
        <Text>One</Text>
    </View>

    <View>
        <Text>Two</Text>
    </View>

    <View>
        <Text>Three</Text>
    </View>

</ScrollView>

The user can move from left to right through the content.

7. horizontal Prop

The horizontal prop changes the main scrolling direction from vertical to horizontal.

<ScrollView horizontal={true}>

    <Text>React</Text>

    <Text>Native</Text>

    <Text>Mobile</Text>

</ScrollView>

You can also write simply:

<ScrollView horizontal>

8. ScrollView with View

View components are commonly placed inside ScrollView.

<ScrollView>

    <View style={styles.card}>
        <Text>Course 1</Text>
    </View>

    <View style={styles.card}>
        <Text>Course 2</Text>
    </View>

</ScrollView>

9. Styling ScrollView

The style prop can be used to style the ScrollView itself.

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20
    }

});
<ScrollView style={styles.container}>

    <Text>
        Scrollable Content
    </Text>

</ScrollView>

10. contentContainerStyle

The contentContainerStyle prop is used to style the content container inside the ScrollView.

const styles = StyleSheet.create({

    content: {
        padding: 20
    }

});
<ScrollView
    contentContainerStyle={styles.content}
>

    <Text>
        Scrollable Content
    </Text>

</ScrollView>

11. Padding in ScrollView

Padding can be added to the content container to create space around the content.

const styles = StyleSheet.create({

    content: {
        padding: 20
    }

});
<ScrollView
    contentContainerStyle={styles.content}
>

    <Text>
        Welcome
    </Text>

</ScrollView>

12. ScrollView with Cards

ScrollView is useful for displaying a small or moderate number of cards.

<ScrollView>

    <View style={styles.card}>
        <Text>HTML Course</Text>
    </View>

    <View style={styles.card}>
        <Text>CSS Course</Text>
    </View>

    <View style={styles.card}>
        <Text>React Course</Text>
    </View>

</ScrollView>

13. ScrollView with Images

Images can also be displayed inside a ScrollView.

<ScrollView>

    <Image
        source={require("./assets/image1.jpg")}
        style={styles.image}
    />

    <Image
        source={require("./assets/image2.jpg")}
        style={styles.image}
    />

</ScrollView>

This can be useful for simple image galleries.

14. ScrollView with Forms

Long forms can use ScrollView so that users can access fields that are below the visible screen.

<ScrollView>

    <Text>Name</Text>

    <TextInput
        placeholder="Enter name"
    />

    <Text>Email</Text>

    <TextInput
        placeholder="Enter email"
    />

    <Text>Address</Text>

    <TextInput
        placeholder="Enter address"
    />

</ScrollView>

15. ScrollView with Long Text

ScrollView is useful for screens containing long descriptions, instructions or terms and conditions.

<ScrollView>

    <Text style={styles.description}>

        React Native is a framework for
        building mobile applications using
        React and JavaScript.

        This text can continue for many
        lines and the user can scroll
        through it.

    </Text>

</ScrollView>

16. ShowsVerticalScrollIndicator

The showsVerticalScrollIndicator prop controls whether the vertical scroll indicator is displayed.

<ScrollView
    showsVerticalScrollIndicator={false}
>

    <Text>
        Scrollable Content
    </Text>

</ScrollView>

Setting it to false hides the vertical indicator.

17. ShowsHorizontalScrollIndicator

The showsHorizontalScrollIndicator prop controls the horizontal scroll indicator.

<ScrollView
    horizontal
    showsHorizontalScrollIndicator={false}
>

    <Text>Item 1</Text>
    <Text>Item 2</Text>
    <Text>Item 3</Text>

</ScrollView>

18. Scroll Event

The onScroll prop can be used to respond to scrolling.

<ScrollView
    onScroll={() => {
        console.log("Scrolling");
    }}
>

    <Text>
        Long Content
    </Text>

</ScrollView>

Scroll events can be useful for advanced UI behavior.

19. Scroll Event Data

The scroll event provides information about the current scroll position.

<ScrollView
    onScroll={(event) => {

        const y =
            event.nativeEvent.contentOffset.y;

        console.log(y);

    }}
>

    <Text>
        Scroll Content
    </Text>

</ScrollView>

The contentOffset can be used to inspect the current scroll position.

20. scrollEventThrottle

The scrollEventThrottle prop can control the frequency of scroll event updates.

<ScrollView
    onScroll={(event) => {
        console.log(
            event.nativeEvent.contentOffset.y
        );
    }}
    scrollEventThrottle={16}
>

    <Text>
        Scroll Content
    </Text>

</ScrollView>

This is mainly useful when an application needs to respond to scrolling continuously.

21. RefreshControl

A ScrollView can support pull-to-refresh behavior using RefreshControl.

import {
    ScrollView,
    RefreshControl
} from "react-native";

It is commonly used when refreshing data from an API.

22. Pull to Refresh

A basic refresh setup can use state to control the refreshing status.

const [refreshing, setRefreshing] =
    useState(false);

const refreshData = () => {

    setRefreshing(true);

    setTimeout(() => {
        setRefreshing(false);
    }, 1000);

};

The ScrollView can then use this state with RefreshControl.

23. ScrollView and Flexbox

Flexbox can be used to arrange content inside a ScrollView.

const styles = StyleSheet.create({

    content: {
        alignItems: "center",
        padding: 20
    }

});
<ScrollView
    contentContainerStyle={styles.content}
>

    <Text>
        Welcome
    </Text>

</ScrollView>

24. ScrollView with Safe Screen Layout

A ScrollView can be placed inside a screen container to create a scrollable page.

<View style={styles.container}>

    <ScrollView
        contentContainerStyle={styles.content}
    >

        <Text style={styles.title}>
            Student Profile
        </Text>

        <Text>
            Student information...
        </Text>

    </ScrollView>

</View>

25. ScrollView vs FlatList

ScrollView FlatList
Good for smaller amounts of content Good for large lists
Renders its children as part of the scrollable content Designed for efficient list rendering
Useful for forms and pages Useful for repeated data
Can contain different types of components Designed around list items

Choosing the correct component becomes important as the amount of data in an application grows.

26. When to Use ScrollView

  • Long profile pages
  • Registration forms
  • Settings screens
  • Terms and conditions
  • Course descriptions
  • Small image galleries
  • Dashboard pages with moderate content
  • Vertical or horizontal content sections

ScrollView is especially useful when the content is known and is not an extremely large collection of repeated items.

27. Complete ScrollView Example

import {
    View,
    Text,
    ScrollView,
    StyleSheet
} from "react-native";

export default function App() {

    return (
        <View style={styles.container}>

            <ScrollView
                contentContainerStyle={styles.content}
            >

                <Text style={styles.title}>
                    React Native Course
                </Text>

                <View style={styles.card}>
                    <Text style={styles.heading}>
                        Introduction
                    </Text>

                    <Text>
                        Learn the basics of React Native
                        and mobile app development.
                    </Text>
                </View>

                <View style={styles.card}>
                    <Text style={styles.heading}>
                        Components
                    </Text>

                    <Text>
                        Learn View, Text, Image and
                        other React Native components.
                    </Text>
                </View>

                <View style={styles.card}>
                    <Text style={styles.heading}>
                        Projects
                    </Text>

                    <Text>
                        Build practical mobile
                        applications.
                    </Text>
                </View>

            </ScrollView>

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1
    },

    content: {
        padding: 20
    },

    title: {
        fontSize: 28,
        fontWeight: "bold",
        marginBottom: 20
    },

    card: {
        padding: 20,
        marginBottom: 15,
        borderRadius: 10,
        backgroundColor: "lightgray"
    },

    heading: {
        fontSize: 20,
        fontWeight: "bold",
        marginBottom: 8
    }

});

28. Common ScrollView Mistakes

  • Using ScrollView for extremely large lists.
  • Forgetting to give the screen an appropriate layout.
  • Using unnecessary nested ScrollViews.
  • Putting too much content into a ScrollView without considering performance.
  • Using horizontal scrolling without considering the available screen width.
  • Ignoring keyboard behavior when building long forms.

Understanding the amount and type of content helps you choose the appropriate scrolling component.

29. Best Practices for ScrollView

  • Use ScrollView for pages and moderate amounts of content.
  • Use contentContainerStyle for content layout.
  • Use horizontal when horizontal scrolling is required.
  • Hide scroll indicators when appropriate.
  • Use scroll events only when they are needed.
  • Consider RefreshControl for pull-to-refresh screens.
  • Use FlatList for large collections of repeated data.
  • Avoid unnecessary nesting of scrollable containers.

30. Complete ScrollView Workflow

ScrollView
 ↓
Choose Direction
 ↓
Vertical / Horizontal
 ↓
Add Content
 ↓
Apply Styles
 ↓
Handle Scroll Events
 ↓
Optional Refresh
 ↓
Check Content Size
 ↓
Use FlatList for Large Lists
 ↓
Complete Scrollable Screen

ScrollView is an important component for creating scrollable mobile screens. You will use it frequently for forms, profiles, settings, course pages and other screens containing moderate amounts of content.

📌 Key Points

  • ScrollView creates a scrollable area.
  • Vertical scrolling is the default behavior.
  • The horizontal prop enables horizontal scrolling.
  • contentContainerStyle styles the scrollable content container.
  • ScrollView can contain Text, View, Image, TextInput and other components.
  • onScroll can be used to respond to scrolling.
  • Scroll indicators can be shown or hidden.
  • RefreshControl can be used for pull-to-refresh functionality.
  • ScrollView is useful for forms and moderate amounts of content.
  • FlatList is generally more suitable for large lists of repeated data.

🧠 Quick Quiz

Question: Which React Native component is commonly used to make a screen's content scrollable?