ScrollView is a core React Native component that allows users to scroll through content that does not fit on the screen. It supports vertical and horizontal scrolling and is best suited for small to medium-sized content.
ScrollView wraps multiple child components and enables scrolling behavior. It can be customized using props like horizontal, showsVerticalScrollIndicator, and contentContainerStyle.
// Import ScrollView and required components
import React from "react";
import { ScrollView, Text, View } from "react-native";
const App = () => {
return (
<ScrollView>
<Text>Item 1</Text>
<Text>Item 2</Text>
<Text>Item 3</Text>
<Text>Item 4</Text>
</ScrollView>
);
};
export default App;
The screen becomes vertically scrollable. As the number of items increases, the user can scroll down to view hidden content smoothly.
Below is the code for a horizontal ScrollView. Underneath, use the Simulator to switch between Vertical and Horizontal modes to see how the layout changes on a phone screen.
// Horizontal ScrollView with styled boxes
import React from "react";
import { ScrollView, View, Text, StyleSheet } from "react-native";
export default function App() {
return (
<ScrollView horizontal>
<View style={styles.box}><Text>A</Text></View>
<View style={styles.box}><Text>B</Text></View>
<View style={styles.box}><Text>C</Text></View>
</ScrollView>
);
}
const styles = StyleSheet.create({
box: {
width: 100,
height: 100,
margin: 10,
backgroundColor: "#2563eb",
alignItems: "center",
justifyContent: "center"
}
});
Current Prop: default (Vertical)
contentContainerStyle for inner styling