Working with Core Components
1. Using View Component
| Property | Description | Default |
|---|---|---|
| style | Applies styling to the View component using StyleSheet or inline styles. | undefined |
| children | Content to be rendered inside the View component. | undefined |
| onLayout | Callback function that is called when the component's layout is calculated. | undefined |
| pointerEvents | Controls whether the View receives pointer events. | 'auto' |
Example: Basic View Usage
import React from "react";
import { View, Text } from "react-native";
const MyComponent = () => {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Hello, Expo!</Text>
</View>
);
};
export default MyComponent;
Note: The View component is the fundamental building block of UI in React Native and Expo, similar to a div in web development.
2. Using Text Component
| Property | Values | Description |
|---|---|---|
| allowFontScaling | true | false |
Whether to allow font scaling based on system settings. Default is true. |
| ellipsizeMode | head | middle | tail | clip |
How to handle text that overflows. Default is tail. |
| numberOfLines | number |
Limits the number of lines displayed. Set to 1 for single line. |
| style | object |
Style object to apply to the Text component. Supports standard React Native style properties. |
| suppressHighlighting | true | false |
Whether to suppress the highlight effect on touch. Default is false. |
Example: Text with Ellipsis
<Text numberOfLines={1} ellipsizeMode="tail">
This is a very long text that will be truncated with an ellipsis at the end.
</Text>
Note: The
Text component in Expo is a thin wrapper around React Native's Text component, and supports all standard React Native styling properties.
3. Using ScrollView
| Property | Description | Default | Notes |
|---|---|---|---|
| horizontal | Whether to scroll horizontally | false |
Set to true for horizontal scrolling |
| showsHorizontalScrollIndicator | Whether to show the horizontal scroll indicator | true |
Set to false to hide the indicator |
| showsVerticalScrollIndicator | Whether to show the vertical scroll indicator | true |
Set to false to hide the indicator |
| scrollEventThrottle | How often to fire the scroll event (in ms) | 16 |
Higher values reduce frequency of scroll events |
| onScroll | Callback when scroll occurs | null |
Receives a NativeSyntheticEvent with scroll info |
| contentContainerStyle | Style for the content container | undefined |
Allows custom styling of the scrollable content |
Example: Basic ScrollView Usage
import React from 'react';
import { ScrollView, Text } from 'react-native';
const MyScrollView = () => {
return (
<ScrollView>
<Text>Item 1</Text>
<Text>Item 2</Text>
<Text>Item 3</Text>
</ScrollView>
);
};
export default MyScrollView;
Example: Horizontal ScrollView
import React from 'react';
import { ScrollView, View, Text } from 'react-native';
const HorizontalScrollView = () => {
return (
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
<View style={{ width: 200, height: 100 }}>
<Text>Item 1</Text>
</View>
<View style={{ width: 200, height: 100 }}>
<Text>Item 2</Text>
</View>
</ScrollView>
);
};
export default HorizontalScrollView;
Note: ScrollView is not suitable for large lists of items. For better performance with many items, use
FlatList.
4. Using FlatList
| Property | Description | Default | Notes |
|---|---|---|---|
| data | Array of data to render | null |
Required |
| renderItem | Function that renders each item | null |
Required |
| keyExtractor | Function to extract a unique key for each item | (item) => item.id |
Recommended for performance |
| horizontal | Render items horizontally | false |
Boolean |
| numColumns | Number of columns to render | 1 |
Use for grid layouts |
| onEndReached | Function called when scrolling near end | null |
Use for infinite scroll |
| onEndReachedThreshold | Distance from end to trigger onEndReached | 0.5 |
Number between 0 and 1 |
Example: Basic FlatList Usage
import React from 'react';
import { FlatList, Text, View } from 'react-native';
const renderItem = ({ item }) => (
<View style={{ padding: 10 }}>
<Text>{item.name}</Text>
</View>
);
const MyFlatList = () => {
const data = [
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' },
{ id: '3', name: 'Item 3' },
];
return (
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={item => item.id}
/>
);
};
export default MyFlatList;
Example: Horizontal FlatList
import React from 'react';
import { FlatList, Text, View } from 'react-native';
const renderItem = ({ item }) => (
<View style={{ width: 100, padding: 10 }}>
<Text>{item.name}</Text>
</View>
);
const HorizontalFlatList = () => {
const data = [
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' },
{ id: '3', name: 'Item 3' },
];
return (
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={item => item.id}
horizontal
/>
);
};
export default HorizontalFlatList;
Example: Grid Layout with FlatList
import React from 'react';
import { FlatList, Text, View } from 'react-native';
const renderItem = ({ item }) => (
<View style={{ width: 100, height: 100, padding: 10 }}>
<Text>{item.name}</Text>
</View>
);
const GridFlatList = () => {
const data = [
{ id: '1', name: 'Item 1' },
{ id: '2', name: 'Item 2' },
{ id: '3', name: 'Item 3' },
{ id: '4', name: 'Item 4' },
];
return (
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={item => item.id}
numColumns={2}
/>
);
};
export default GridFlatList;
Note: FlatList is optimized for large lists and should be preferred over ScrollView with a list of items.
5. Using SectionList
SectionList is a React Native component that renders a scrollable list of data grouped into sections. It's optimized for large lists with dynamic content and is ideal for displaying structured data in a scrollable interface.
| Property | Description | Values |
|---|---|---|
| sections | An array of objects representing each section. Each object must include a data array and a title. |
Array<{ title: string, data: any[] }> |
| renderItem | A function that renders each item in a section. Receives an object with item, section, and index. |
(info: { item: any, section: Section, index: number }) => ReactElement |
| renderSectionHeader | A function that renders the header for each section. Receives an object with section. |
(info: { section: Section }) => ReactElement |
| keyExtractor | A function that generates a unique key for each item in the list. | (item: any, index: number) => string |
| stickySectionHeadersEnabled | Whether to stick section headers to the top of the list as it scrolls. | true | false |
Example: Basic SectionList Usage
import React from 'react';
import { SectionList, Text, View } from 'react-native';
const data = [
{
title: 'A',
data: ['Apple', 'Apricot'],
},
{
title: 'B',
data: ['Banana', 'Blueberry'],
},
];
const renderItem = ({ item }) => (
<View style={{ padding: 10 }}>
<Text>{item}</Text>
</View>
);
const renderSectionHeader = ({ section }) => (
<View style={{ backgroundColor: '#f0f0f0', padding: 10 }}>
<Text style={{ fontWeight: 'bold' }}>{section.title}</Text>
</View>
);
const MySectionList = () => (
<SectionList
sections={data}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}
keyExtractor={(item, index) => item + index}
/>
);
export default MySectionList;
6. Using Image Component
| Property | Description | Default | Notes |
|---|---|---|---|
| source | Image to display. Can be a local file or remote URL. | None | Required |
| style | Style object or array of styles to apply to the image. | undefined | Supports flexbox and layout properties |
| resizeMode | How to resize the image when it's smaller or larger than its container. | "contain" |
Options: cover, contain, stretch, repeat, center |
| blurRadius | Applies a blur effect to the image (iOS only). | 0 | Requires expo-blur package |
Example: Basic Image Usage
<Image
source={{ uri: "https://example.com/image.jpg" }}
style={{ width: 200, height: 200 }}
resizeMode="cover"
/>
Example: Local Image
<Image
source={require("./assets/images/logo.png")}
style={{ width: 100, height: 100 }}
/>
Note: On web, image URLs must be served with proper CORS headers to display correctly.
Warning: Using large images without proper sizing can cause performance issues.
7. Using ImageBackground
| Property | Values | Description |
|---|---|---|
| source | imageSource |
Image to display as background. Can be a local image or remote URL. |
| resizeMode | cover | contain | stretch | repeat | center |
How to resize the image to fit the container. |
| imageRef | React.Ref |
Reference to the underlying image component. |
Example: Basic ImageBackground Usage
import React from 'react';
import { ImageBackground, View, Text } from 'react-native';
const BackgroundExample = () => {
return (
<ImageBackground
source={{ uri: 'https://example.com/background.jpg' }}
style={{ width: '100%', height: '100%' }}
resizeMode="cover"
>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ color: 'white', fontSize: 20 }}>Hello World</Text>
</View>
</ImageBackground>
);
};
export default BackgroundExample;
Note: ImageBackground is a wrapper around the React Native
Image component, so all props supported by Image are also supported here.
8. Using TextInput
| Property | Description | Default | Notes |
|---|---|---|---|
| value | Current text value of the input | '' |
Controlled component |
| onChangeText | Callback when text changes | () => {} |
Receives new text as argument |
| placeholder | Hint text when input is empty | '' |
Appears in light gray |
| multiline | Allow multiple lines | false |
Useful for large text input |
| secureTextEntry | Mask input (e.g., password) | false |
Useful for sensitive data |
| keyboardType | Keyboard type to show | 'default' |
Options: 'default', 'numeric', 'email-address', etc. |
Example: Basic TextInput Usage
<TextInput
value={text}
onChangeText={setText}
placeholder="Enter your name"
style={{ borderWidth: 1, padding: 8 }}
/>
Example: Secure Text Input
<TextInput
value={password}
onChangeText={setPassword}
placeholder="Enter password"
secureTextEntry
style={{ borderWidth: 1, padding: 8 }}
/>
9. Using Button Component
| Property | Values | Description |
|---|---|---|
| title | string |
The text to display on the button |
| onPress | function |
Function to call when the button is pressed |
| color | string |
Background color of the button (e.g., 'red', '#00ff00') |
| disabled | boolean |
If true, the button is disabled and cannot be pressed |
| accessibilityLabel | string |
Text for accessibility tools to read aloud |
Example: Basic Button Usage
import { Button } from 'react-native';
const MyButton = () => {
const handlePress = () => {
console.log('Button pressed!');
};
return (
<Button
title="Press Me"
onPress={handlePress}
color="#007AFF"
/>
);
};
Example: Button with Disabled State
import { Button } from 'react-native';
const DisabledButton = () => {
return (
<Button
title="Cannot Press"
onPress={() => {}}
disabled={true}
/>
);
};
10. Using Pressable
Pressable is a core component in Expo that provides a flexible way to handle touch interactions and user feedback. It wraps other components and allows you to define custom behavior for press, long press, and hover events.
| Property | Description | Values |
|---|---|---|
| onPress | Function called when the component is pressed | function |
| onLongPress | Function called when the component is long-pressed | function |
| onHoverIn | Function called when the pointer enters the component (web only) | function |
| onHoverOut | Function called when the pointer leaves the component (web only) | function |
| style | Style object or array of styles to apply to the component | object | array |
Example: Basic Pressable Usage
<Pressable
onPress={() => console.log('Pressed!')}
style={({ pressed }) => [
styles.button,
{ backgroundColor: pressed ? '#0055aa' : '#0077cc' }
]}
>
<Text style={styles.text}>Press Me</Text>
</Pressable>
Example: Pressable with Long Press
<Pressable
onPress={() => console.log('Short press')}
onLongPress={() => console.log('Long press')}
style={styles.longPressButton}
>
<Text>Long Press Me</Text>
</Pressable>
11. Using TouchableOpacity
| Property | Description | Default | Notes |
|---|---|---|---|
| activeOpacity | Opacity when the touchable is active | 0.2 |
Value between 0 and 1 |
| disabled | Disables the touchable component | false |
When true, no interaction occurs |
| onPress | Callback when the touchable is pressed | null |
Function to execute on press |
| onPressIn | Callback when the touchable is pressed down | null |
Function to execute on press down |
| onPressOut | Callback when the touchable is released | null |
Function to execute on press release |
Example: Basic TouchableOpacity Usage
<TouchableOpacity
onPress={() => console.log('Button pressed')}
activeOpacity={0.5}
>
<Text>Press Me</Text>
</TouchableOpacity>
Note:
TouchableOpacity is ideal for creating interactive elements that need to provide visual feedback on press, such as buttons or links.
12. Using ActivityIndicator
| Property | Values | Description |
|---|---|---|
| animating | true | false |
Controls whether the indicator is animating. Defaults to true. |
| color | string |
The color of the indicator. Defaults to #000 on iOS and #fff on Android. |
| size | 'small' | 'large' | number |
The size of the indicator. Defaults to 'small'. |
Note: On web,
ActivityIndicator uses a spinner from react-native-indicator.