26
loading...
This website collects cookies to deliver better user experience
expo init ReactNativeList
into my command line (Terminal on Mac). This will show a few options that we can select from to generate a boilerplate React Native template that's preconfigured to be served with Expo. We definitely want to choose a Managed option, because those have the built steps already set up for us. I chose Typescript but it doesn't matter much for this small application.npm start
or yarn start
to serve the application with Expo. When this is successful it should show a QR code and a few other details about what's running on your computer..js
or .tsx
extension depending on whether or not you chose vanilla JS or Typescript. I need practice with Typescript so I chose TS.App
open:react-native
package comes with a number of core components that we can use easily across devices. Today we'll use the FlatList
component.App
like so:import { StyleSheet, Text, View, FlatList } from 'react-native';
name
and quantity
properties that I've typed as strings. Again, this is in Typescript:const groceries: {
name: string;
quantity: string;
}[] = [
{name: 'Broccoli', quantity: '8oz'},
{name: 'Pasta', quantity: '12oz'},
{name: 'Crushed Tomatoes', quantity: '22oz'},
{name: 'Basil', quantity: '4oz'},
{name: 'Garlic', quantity: '3oz'},
{name: 'Baguette', quantity: '1 Large'},
];
<FlatList />
component into our JSX. This React Native component takes a few required props for it to work. First, we need to give it the data
prop to tell the list what our data source is, in our case it's our object groceries
.renderItem
prop to tell the component what JSX we want it to render for each list item. I know this is a bit confusing if you're used to plain React. Remember, we need this to compile into multiple languages on multiple devices, so we're trying to tell React Native how to do that. The prop you pass in should be a JSX component and it assumes you are iterating over the array passed into the data
prop so you can pass a variable representing a single list item in as an argument, which I simply call item
here. I'm going render a simple item with item.name
and item.quantity
as strings inside, and voila... here's what that prop should look like now:renderItem={({item}) => {
return <Text style={ styles.listItem }>{ item.name } - Quantity: { item.quantity }</Text>
}}
ListItem
component is called keyExtractor
. Essentially, this assures each item in the array has a unique JSX element assigned to it. We could get around this by making sure there is a key
property with unique values on each item in our groceries
array, but that would be unlikely to happen if we were using a real data source. I'm going to use the name.return (
<View style={styles.container}>
<View style={ styles.headerSafe }></View>
<Text style={styles.headerText}>My React Native Grocery List</Text>
<FlatList
data={ groceries }
keyExtractor={ grocery => grocery.name }
renderItem={({item}) => {
return <Text style={ styles.listItem }>{ item.name } - Quantity: { item.quantity }</Text>
}}
/>
</View>
);
style
props for my JSX elements. This is totally optional. I'll skip over that since styling elements is outside the scope of this article but it's just standard inline React styling with CSS in JS.View
component with nothing inside it as the first child of our main container View element. This was just a hacky workaround to make sure the other elements are shown below the hardware of an iPhone. Don't worry about it.)