Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 71 ยท Components and Interaction

ScrollView and Keyboard Handling

Learn ScrollView, RefreshControl, KeyboardAvoidingView, Keyboard.dismiss and keyboardShouldPersistTaps so forms stay visible and usable when the keyboard opens.

Two things catch almost every new React Native developer. First, content taller than the screen does not scroll by itself: a View simply runs off the bottom. Second, when the on-screen keyboard slides up, it covers whatever was at the bottom, often the very input the person is typing into. ScrollView solves the first problem, and a small set of keyboard tools solves the second.

On the web the browser handles both for you. On a phone, you decide how the layout reacts, and the right answer depends on the platform.

What runs on this site. Scrolling physics and the software keyboard belong to the native platform, so the real React Native code is read-only here. The runnable examples are stand-ins: a scrollable div that reports its offset the way onScroll does, and a phone-frame simulation of keyboard avoidance, which computes the visible area with the same arithmetic. Real keyboards differ by device, so always test on a phone or emulator.

ScrollView

ScrollView is a scrolling container. Put children inside it, and when they are taller than the space available, the user can drag.

JSX
import { ScrollView, Text } from 'react-native';

export function Terms({ paragraphs }) {
  return (
    <ScrollView
      style={{ flex: 1 }}
      contentContainerStyle={{ padding: 16, gap: 12 }}
    >
      {paragraphs.map((p) => (
        <Text key={p.id}>{p.text}</Text>
      ))}
    </ScrollView>
  );
}

Two style props are easy to mix up. style styles the scroll container itself (its size and position), so give it flex: 1 to take the available height. contentContainerStyle styles the inner view that holds your children, which is where padding and gap belong. Padding in style would be outside the scrolling area and would not move with the content.

Useful props:

  • horizontal scrolls sideways instead (a row of chips, a carousel).
  • showsVerticalScrollIndicator={false} hides the scrollbar.
  • pagingEnabled snaps to full-screen pages.
  • stickyHeaderIndices={[0]} pins the child at those indexes to the top while scrolling.
  • onScroll reports the position in event.nativeEvent.contentOffset. On iOS, set scrollEventThrottle={16} or you only get one event per scroll.
  • nestedScrollEnabled lets a scroll view inside another scroll on Android.

To scroll from code, use a ref: ref.current?.scrollTo({ y: 0, animated: true }) or scrollToEnd().

ScrollView is not for long lists

A ScrollView renders all of its children at once, even those far off screen. For ten paragraphs that is fine. For a thousand products it is slow and uses a lot of memory. Use FlatList for long or dynamic lists, because it renders only what is near the screen, as you will see in FlatList Essentials. Use ScrollView for content with a modest, known size: settings screens, forms, articles.

ScrollView at a glance

style Size of the container. Often flex 1.
contentContainerStyle Padding and gap for the content.
horizontal Scroll sideways.
Not for long lists Everything renders at once.
1 / 2
A scroll container that reports its offset (stand-in)

Pull to refresh

Give a ScrollView (or FlatList) a refreshControl and it gets the platform's pull-to-refresh gesture:

JSX
import { useState, useCallback } from 'react';
import { ScrollView, RefreshControl } from 'react-native';

export function Feed() {
  const [refreshing, setRefreshing] = useState(false);

  const onRefresh = useCallback(async () => {
    setRefreshing(true);
    await reloadFeed();
    setRefreshing(false);
  }, []);

  return (
    <ScrollView
      refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}
    >
      {/* content */}
    </ScrollView>
  );
}

You own the refreshing flag: set it to true when loading starts and back to false when it ends. We use this again in SectionList, Pull to Refresh and Infinite Scroll.

The keyboard problem

When an input near the bottom gains focus, the keyboard appears and covers part of the screen. There are four standard tools.

KeyboardAvoidingView

KeyboardAvoidingView shrinks or shifts its content by the keyboard's height, keeping the form visible. Its behavior prop is platform specific:

JSX
import { KeyboardAvoidingView, Platform, ScrollView, TextInput } from 'react-native';

export function LoginForm() {
  return (
    <KeyboardAvoidingView
      style={{ flex: 1 }}
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
      keyboardVerticalOffset={Platform.OS === 'ios' ? 64 : 0}
    >
      <ScrollView
        contentContainerStyle={{ padding: 16 }}
        keyboardShouldPersistTaps="handled"
        keyboardDismissMode="on-drag"
      >
        <TextInput placeholder="Email" />
        <TextInput placeholder="Password" secureTextEntry />
      </ScrollView>
    </KeyboardAvoidingView>
  );
}

On iOS, padding is the usual choice. On Android, the system can already resize the window when the keyboard opens (Expo's default is the resize setting), so height or no avoiding view at all may be enough. Newer Android versions with edge-to-edge screens change this again, so test on real devices. Use keyboardVerticalOffset when something sits above your view, like a navigation header, to correct the calculation.

keyboardShouldPersistTaps

By default, the first tap outside a focused input only closes the keyboard, and a button underneath never sees the tap. That is the classic "I have to press Save twice" bug. Set keyboardShouldPersistTaps on the ScrollView:

  • never (default): taps outside the keyboard dismiss it and are swallowed.
  • handled: a tap on a child that handles it (such as a button) is delivered and the keyboard stays open; a tap on empty space still dismisses it. This is the best choice for forms.
  • always: the keyboard never closes on tap.

Dismissing the keyboard

Close the keyboard from code with Keyboard.dismiss(), for example after a successful submit or when the person taps the empty background. Also useful: keyboardDismissMode="on-drag" on a ScrollView closes it as soon as the user scrolls. You can listen to Keyboard.addListener('keyboardDidShow', ...) events for the keyboard's height, and on iOS automaticallyAdjustKeyboardInsets makes a ScrollView add the inset by itself. For complex screens, libraries such as react-native-keyboard-controller offer smoother, more consistent behavior across platforms.

Keyboard avoidance math (stand-in)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should padding between the scrolled content and the screen edges go?

    contentContainerStyle styles the inner content view, so padding scrolls with the content. style styles the outer container's size and position.
  2. 2. Why is ScrollView a poor choice for a list of thousands of items?

    ScrollView mounts every child immediately. FlatList virtualizes, rendering only the rows near the visible area.
  3. 3. A Save button below a focused input needs two taps. Which prop on the ScrollView fixes it?

    With the default 'never', the first tap only dismisses the keyboard. 'handled' delivers taps to children that handle them, such as buttons.
  4. 4. Which behavior value is the common choice for KeyboardAvoidingView on iOS?

    On iOS, 'padding' adds bottom padding equal to the keyboard height. Android often needs 'height' or nothing, depending on the window resize setting.

Summary

  • ScrollView scrolls content larger than the screen; style sizes the container, contentContainerStyle styles the content (padding, gap).
  • It renders everything at once, so use FlatList for long lists.
  • Add pull-to-refresh with refreshControl and a refreshing flag you control; on iOS set scrollEventThrottle to get regular onScroll events.
  • Keep inputs visible with KeyboardAvoidingView (platform-specific behavior) and a keyboardVerticalOffset for headers.
  • Use keyboardShouldPersistTaps="handled", keyboardDismissMode and Keyboard.dismiss() for smooth forms.
  • On this site the real code is read-only; the TryIts are stand-ins for scrolling and keyboard arithmetic.

Next, put things on top of the screen with Modals, Alerts and Bottom Sheets.