Benefits of React Native
Benefits of React Native
Now that you know what React Native is, let’s explore it further and focus on the benefits that using
React Native can bring. This way, you will be confident about the tool you will use to build the Little
Lemon app.
In this reading, you will learn about the benefits of React Native and how to explain why it is a
practical choice for building cross-platform mobile apps. This will help you decide if React Native is a
good fit for your project.
1. Uses JavaScript
The first benefit of React Native is that it is entirely coded in JavaScript. This means you can use
your existing JavaScript skills and put them to use when building React Native apps.
JavaScript has been the most popular programming language in the world for many years, and its
compatibility with React Native is a huge perk for helping JavaScript developers become mobile
developers.
2. Uses React
The next benefit of using React Native is, as you probably guessed, that it uses React.
React Native apps are written entirely in JavaScript using React. React is the most popular and in-
demand web framework today and is used by several major companies and startups to build their
web applications. With React Native, you can use React to build mobile applications.
You can also use all the principles learned in React with React Native. This means you can use the
component-based architecture that comes with React. Code sharing between mobile and web
platforms becomes more manageable, saving development costs.
With React Native, you build a real native mobile app. It is impressive since you are coding in
JavaScript and rendering components native to the platform. This is one of the reasons why apps
built using React Native have a superior user experience (UX).
React Native provides platform-agnostic components that map directly to the platform’s native user
interface (UI) building blocks. This results in high-quality native apps that offer a great user
experience.
4. Cost Effective
At this point, it may be no surprise to learn that React Native could save you money!
This is because React Native is a cost-effective option for teams. Teams can be smaller in size, with
developers working cross-platform on the same code base. There is no need to support separate
codebases for iOS and Android.
This results in faster development time. This also means that the code is easier to test and debug.
Once you find a bug, it is simultaneously removed in iOS and Android. This eliminates the need to
maintain multiple codebases.
5. Developer Experience
The last benefit is that, unlike developing traditional native mobile apps, developing React Native
apps is a far superior developer experience. You will enjoy the ease of development, all the
developer tools it comes with, and how straightforward it is to debug.
Let’s explore some features you can utilize to enjoy a great developer experience.
Fast Refresh
React Native supports fast refresh, which gives almost instant feedback for your changes in React
Components while the app is running on an emulator. The quick refresh feature is a big time-saver
for developers. You can see your changes on the emulator as soon as you hit save!
Easy Debugging
Debugging React Native apps is simple since you are essentially debugging JavaScript code. You
can use the Chrome developer tools to debug the JavaScript code. In addition, you can use the
React Developer tools to debug the React Component hierarchy.
Over-the-Air Updates
The next feature, which is another big time-saver, is over-the-air updates. In traditional native apps,
both iOS and Android, the release process can be tedious and getting approval from the app store
can take several days. With React Native, you may be able to avoid this wait, depending on your
changes. App Center’s Code Push is a cloud service that enables React Native developers to deploy
mobile app updates directly to the users’ devices. It acts as a central repository to which developers
can publish updates, and those apps can query for updates. This means small features or bug fixes
can be made available to users immediately, without redistribution through the respective app
stores. Features like Over the Air (OTA) updates make the React Native developer experience
seamless.
Conclusion
The React Native community is growing incredibly fast, and now is a great time to learn.
You should now be more familiar with the benefits of using React Native and understand why it is a
fantastic tool for building cross-platform apps. Hopefully you’ll find it useful for your next project!
Procedure
Step 1: Create Footer Component
This component will print the text needed for the footer.
components/LittleLemonFooter.js
Below you'll find the code used to style the footer component.
Finally, you’ll need to call the footer component from the App component by applying the
following code:
The code block below outlines the outcome of the exercise in its entirety.
components/LittleLemonFooter.js
This reading will explore the code you can write to build the LittleLemonHeader component
using the View and Text component.
LittleLemonHeader.js
The component renders a parent View, which has been given a background color and a flex
property of 0.2. This means that the header component will occupy 20 percent of the total space
on the screen.
Next, you will see a Text component that renders the text Welcome. Here the Text
component has been given various styling items — padding of 40 pixels, a font size of 30 and a
text color of black. You will also notice another Text component nested within the parent Text
component. The component renders the text Little Lemon in bold.
The nested Text component inherits all the properties and styling from the parent Text
component. Notice that the number of visible lines have been set to 3 for the parent Text
component.
And that’s it! The Little Lemon header component is complete.
Here the parent view has a flex property of 1 and a background color.
App.js
import * as Reactfrom'react';
import{ View } from'react-native';
Overview
Previously, you were introduced to two core components: View and Text. If you recall, View
is the basic building block of the user interface that houses other elements, while Text is used to
display text on the screen.
In this activity, you will use these components to create a Welcome screen for the Little Lemon
app. In the process you’ll learn best practices for efficiently building React Native screens in a
mobile app.
Scenario
The Little Lemon app already has a header and footer component, and now you have been asked
to create a Welcome screen for the app. This screen will contain a welcome message as
demonstrated below:
Starter Code
Use the code below and build upon it to complete this exercise. The code can be downloaded
from the zipped folder below:
Instructions
Step 1: Create a Welcome Screen
Your first step is to create a Welcome screen. Create a file named WelcomeScreen.js within
your source project folder. This will contain the code to display the welcome text.
Little Lemon is a charming neighborhood bistro that serves simple food and classic
cocktails in a lively but casual environment. We would love to hear more about your
experience with us!
Hint: Think about how to structure this with components. Where should the Text component be
relative to View?
The next step is to add styles to your Welcome screen. Don't forget that this is a creative
process, so feel free to choose any styles, font sizes and colors that you think suit the screenshot
given in the scenario.
Hint: You can follow the same styling patterns and colors used so far in the starter code.
In this step, you will call the Welcome screen that you created from the App component. This
ensures that the Welcome screen is rendered on the app.
Hint: Have you imported all the necessary components for rendering?
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to create
screens within your React Native app. You will demonstrate how to use some of the core
components in React Native, such as View and Text while building the Welcome screen for
Little Lemon.
The code block below outlines the outcome of the exercise in its entirety.
WelcomeScreen.js
First, you’ll need to create a WelcomeScreen.js file within the source folder of your project
file.
The next step is to style the Welcome Screen appropriately. Notice that there are different
styles applied for the title and the subheading.
Finally, you can call the WelcomeScreen component from the App component with the
following code:
And with that, the screen you created in the WelcomeScreen component should be rendered
through the App component!
Question 3
Which one of the following code snippets correctly represents the Little Lemon app’s Welcome
screen, as created in the previous exercise?
Well done! ScrollView must be bounded by a height, because it contains unbounded height
children into a bounded container.
Correct
Well done! ScrollView comes as part of the React Native package, and it must be bounded in
height in order to work.
You would like to create a list for your app that is scrolled horizontally. Which ScrollView prop
would you pass to accomplish this?
Well done! When the horizontal prop is set to ‘true’, the list items are set horizontally.
Previously, you were introduced to the ScrollView component, a core component of React
Native. Now, let’s say you want to display a scrollable list of menu items for the Little Lemon
restaurant.
In this reading, you’ll learn how to do this by exploring the code needed to build the Little
Lemon scrollable menu items.
Create a List
The first step is to create a MenuItems.js file within the components folder of your project.
You can name this component whatever you wish. This component is going to display the
scrollable list of menu items.
Second, you will declare an array of menu items for Little Lemon. This array will be used later to
render the menu items. Observe the code below for the MenuItems component, which contains
the array.
MenuItems.js
const menuItemsToDisplay = [
'Hummus \n Moutabal \n Falafel \n Marinated Olives \n Kofta \n Eggplant Salad \
n Lentil Burger \n Smoked Salmon \n Kofta Burger \n Turkish Kebab \n Fries \n But
tered Rice \n Bread Sticks \n Pita Pocket \n Lentil Soup \n Greek Salad \n Rice P
ilaf \n Baklava \n Tartufo \n Tiramisu \n Panna Cotta',
];
In addition, the ScrollView has been provided with the prop indicatorStyle with a value
of white. This means that the scroll bar indicator will be white in color. This can be swapped to
black if the background color is light.
And that’s it! The ScrollView is now set up, and will render the menu items from the array.
App.js
You should now have a deeper understanding of how to use the ScrollView component to build a
scrollable list of items within your app.
Exercise: Build a scrollable component
Overview
Recently, you learned how to use the ScrollView component in React Native. By now, you
should know that ScrollView can be used to create a scrollable view for lists that are too long
to be contained in one screen.
In this activity, you will use a ScrollView component to create a scrollable Welcome screen
for the Little Lemon app. The skills you will practice here can be applied to building other
scrollable views for your React Native apps.
Scenario
The Little Lemon app has a Welcome screen that is demonstrated in the image below.
Currently, this screen is not scrollable. Because of the large font size used for the text, the
contents do not fit within the confines of one screen, and users cannot view the content in full.
In order to make the content accessible, you have been asked to enhance the Welcome screen
from the previous exercise to make it scrollable. You should also make sure that the scroll
indicator is white, so it matches the Welcome screen styles.
Starter Code
In the first screenshot, the emulator is without a scroll bar. It is then followed by an example of
the emulator where the scrollbar is visible after you have run the code and followed the
instructions.
Next, you will include a prop to the ScrollView component to ensure that the scroll indicator
is white in color.
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to create
scrollable views. You will demonstrate how to use the ScrollView component from React
Native while building the Welcome screen for Little Lemon.
The code block below outlines the outcome of the exercise in its entirety.
WelcomeScreen.js
import * as React from 'react';
import { ScrollView, Text } from 'react-native';
The font size on the Welcome screen is fairly large and needs to be scrollable. The code above
produces the following output on the emulator with a scrollable screen:
Procedure
Next, review the breakdown of the individual steps below.
Modify the Welcome Screen to use ScrollView component instead of the View component
for the parent view.
Pass the prop indicatorStyle to the ScrollView component and give it the value white.
This will ensure that the scroll indicator is white in color.
...
Styling using StyleSheet
Previously, you found that you can use the StyleSheet API to abstract away styles. By
moving styles away from the component’s render, you are making the code easier to understand.
In this reading, you’ll learn about the code used to create detailed StyleSheets for the Little
Lemon app. You can then use this to quickly extract styles for the different app elements, such as
containers and headers.
You can create stylesheets specific to each component and keep the stylesheets within the same
file as the component. This will keep the styles closer to the component and makes it easier to
reference.
To create a stylesheet, you will use the create method. The styles can be stored within a
const, as shown below:
All the styles are abstracted away using the StyleSheet API across all the components you have
created so far.
App.js
const menuItemsToDisplay = [
'Hummus \nMoutabal \nFalafel \nMarinated Olives \nKofta \nEggplant Salad \nLent
il Burger \nSmoked Salmon \nKofta Burger \nTurkish Kebab \nFries \nButtered Rice
\nBread Sticks \nPita Pocket \nLentil Soup \nGreek Salad \nRice Pilaf \nBaklava \
nTartufo \nTiramisu \nPanna Cotta',
];
In this exercise, you will style a component and add the styles to the StyleSheet API.
Scenario
The Little Lemon app currently has some styling applied to the components. However, you have
been asked to update the styling to achieve a different appearance. You should utilize the
StyleSheet API to abstract the styles from the component’s render. Make sure that the component
code is readable and clean.
Starter Code
You can download the starter code for this exercise from the zipped folder below:
Instructions
Step 1: Update Styles of Components to match Scenario
Your first step in this exercise is to update the styles of the given code to match the screenshot
given in the scenario. Make sure to use the appropriate colors, padding and overall styling theme
shown in the scenario.
In this step, you will extract all the styles per component into its own StyleSheet API. This
will ensure that your component code is readable. A best practice is to provide meaningful names
to all your styles so that you can easily identify the style or where it is used.
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to style React
Native components. You will have demonstrated how to use the StyleSheet API to abstract
away styles while keeping the component code clean and readable.
Mark as completed
Like
Dislike
Report an issue
The image below demonstrates the expected output of the exercise. Follow each step below to
explore the solution code.
Procedure
Here is a breakdown of the individual steps.
In this step, you will update the styles in each component to match the screenshots provided. The
components now have different background colors, font style and text color. Update the
necessary styles to match the recommended output.
LittleLemonFooter.js
In this step, ensure to extract the styles away from the component’s render and instead add them
to the StyleSheet const. This way the styles are given meaningful names and do not mix with
the JSX code within the component.
Refactor WelcomeScreen.js
Let’s start with a list of menu items you want to display on the Little Lemon menu. It is declared as
an array of objects. Each item contains a name representing the menu item’s name and a unique id.
const menuItemsToDisplay = [
{ name: 'Hummus', id: '1A' },
{ name: 'Moutabal', id: '2B' },
{ name: 'Falafel', id: '3C' },
{ name: 'Marinated Olives', id: '4D' },
{ name: 'Kofta', id: '5E' },
{ name: 'Eggplant Salad', id: '6F' },
{ name: 'Lentil Burger', id: '7G' },
{ name: 'Smoked Salmon', id: '8H' },
{ name: 'Kofta Burger', id: '9I' },
{ name: 'Turkish Kebab', id: '10J' },
{ name: 'Fries', id: '11K' },
{ name: 'Buttered Rice', id: '12L' },
{ name: 'Bread Sticks', id: '13M' },
{ name: 'Pita Pocket', id: '14N' },
{ name: 'Lentil Soup', id: '15O' },
{ name: 'Greek Salad', id: '16Q' },
{ name: 'Rice Pilaf', id: '17R' },
{ name: 'Baklava', id: '18S' },
{ name: 'Tartufo', id: '19T' },
{ name: 'Tartufo', id: '20U' },
{ name: 'Tiramisu', id: '21V' },
{ name: 'Panna Cotta', id: '22W' },
];
To use the FlatList component, you will need to import it directly from the React Native package,
since it is a core component. Use the following code snippet:
The FlatList component has two required props you will need to pass to it as a bare
minimum.
The first prop is the data that accepts a plain array. This array contains the list of items to
display. The second prop is the renderItem. The renderItem takes an item from the data
and renders it into the list.
You can also provide additional metadata like index to the renderItem. Here index is a
number that corresponds to a particular item in the data array.
Now within the MenuItems component, you are going to render the FlatList component as
follows:
return (
<View style={menuStyles.container}>
<Text style={menuStyles.headerText}>View Menu</Text>
<FlatList data={menuItemsToDisplay} keyExtractor={item => item.id} renderIt
em={renderItem}></FlatList>
</View>
);
};
Notice here that you have provided the data prop with the name of the array you already have
defined, which is menuItemsToDisplay. The renderItem method calls a call-back method
which renders another component called Item. You can also see an additional prop called the
keyExtractor being passed to the FlatList component. It instructs the list to use the id of
each item as React keys.
In the code above, the Item component takes name as the prop. And it renders name within a
parent view. The Item component is rendered for every item in the array until it reaches the end.
This is a much better way to render large lists performantly than the ScrollView component.
The FlatList renders items lazily, which means it only renders items actually seen on the
screen. Once you start scrolling up or down, those items off the screen are removed, and the new
items are rendered. This form of lazy rendering is very performant and effective when rendering
large lists in mobile apps.
Below, you’ll find the styles used in this component for your reference:
const menuItemsToDisplay = [
{ name: 'Hummus', id: '1A' },
{ name: 'Moutabal', id: '2B' },
{ name: 'Falafel', id: '3C' },
{ name: 'Marinated Olives', id: '4D' },
{ name: 'Kofta', id: '5E' },
{ name: 'Eggplant Salad', id: '6F' },
{ name: 'Lentil Burger', id: '7G' },
{ name: 'Smoked Salmon', id: '8H' },
{ name: 'Kofta Burger', id: '9I' },
{ name: 'Turkish Kebab', id: '10J' },
{ name: 'Fries', id: '11K' },
{ name: 'Buttered Rice', id: '12L' },
{ name: 'Bread Sticks', id: '13M' },
{ name: 'Pita Pocket', id: '14N' },
{ name: 'Lentil Soup', id: '15O' },
{ name: 'Greek Salad', id: '16Q' },
{ name: 'Rice Pilaf', id: '17R' },
{ name: 'Baklava', id: '18S' },
{ name: 'Tartufo', id: '19T' },
{ name: 'Tartufo', id: '20U' },
{ name: 'Tiramisu', id: '21V' },
{ name: 'Panna Cotta', id: '22W' },
];
return (
<View style={menuStyles.container}>
<Text style={menuStyles.headerText}>View Menu</Text>
<FlatList
data={menuItemsToDisplay}
keyExtractor={(item) => item.id}
renderItem={renderItem}></FlatList>
</View>
);
};
1. e 2
PreviousNext
In this exercise, you will update the Little Lemon app to display a large list of menu items and each
item’s price. You will accomplish this by using a FlatList component.
Scenario
The Little Lemon app needs to display a large list of menu items on the screen, along with the price
of each item. You have been asked to use the FlatList component to render these menu items
efficiently. You should also make sure that the component you write is readable and clean.
Below is the array of menu items and the price for each item. Note that each item also has an id
value.
const menuItemsToDisplay = [
{ name: 'Hummus', price: '$5.00', id: '1A' },
{ name: 'Moutabal', price: '$5.00', id: '2B' },
{ name: 'Falafel', price: '$7.50', id: '3C' },
{ name: 'Marinated Olives', price: '$5.00', id: '4D' },
{ name: 'Kofta', price: '$5.00', id: '5E' },
{ name: 'Eggplant Salad', price: '$8.50', id: '6F' },
{ name: 'Lentil Burger', price: '$10.00', id: '7G' },
{ name: 'Smoked Salmon', price: '$14.00', id: '8H' },
{ name: 'Kofta Burger', price: '$11.00', id: '9I' },
{ name: 'Turkish Kebab', price: '$15.50', id: '10J' },
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00', id: '12L' },
{ name: 'Bread Sticks', price: '$3.00', id: '13M' },
{ name: 'Pita Pocket', price: '$3.00', id: '14N' },
{ name: 'Lentil Soup', price: '$3.75', id: '15O' },
{ name: 'Greek Salad', price: '$6.00', id: '16Q' },
{ name: 'Rice Pilaf', price: '$4.00', id: '17R' },
{ name: 'Baklava', price: '$3.00', id: '18S' },
{ name: 'Tartufo', price: '$3.00', id: '19T' },
{ name: 'Tiramisu', price: '$5.00', id: '20U' },
{ name: 'Panna Cotta', price: '$5.00', id: '21V' },
];
You will use this array to render the items within the screen. Also, use the id for each item as
the key to the FlatList component.
The screenshots below demonstrate how your app should look after you complete this exercise:
The colors displayed in the images above can be applied with the following values:
Starter Code
Use the starter code and build upon it to complete this exercise. You can download the code from
this zipped folder:
Instructions
Step 1: Create a component to display menu items and use FlatList
To begin, create a new component to display the menu items and prices. You can call this
component MenuItems.
Within this component, use the array menuItemsToDisplay provided in the scenario to pass
to the data prop of the FlatList component.
Then, configure the renderItem prop of the FlatList component to render each item’s
name and the price per item. Provide the id as the key to the FlatList.
Hint: You can create multiple components within the same file to keep code clean.
Next, you will style the new component you created to match the screenshots shown above. It is
best to do this by adding styles to the StyleSheet, and then calling them within the component.
Make sure to provide meaningful names to all your styles, making them easier to reference later.
For example, headerText is a more useful name than style1.
Hint: You may want to use the following style props: backgroundColor and fontSize.
When using a StyleSheet, don’t forget to define it within the file first by using const.
To finish this exercise, you’ll need to render the component you just created so that it renders the
menu items on the screen. Render this component from the App component and check the results
in the emulator to verify that it renders correctly.
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to configure
and utilize the FlatList component in order to render a large list of items.
Explore the SectionList Component
In an earlier video, you learned how to use the SectionList component to render large lists with
section headers efficiently. In this reading, let’s explore the code from that video to build the
SectionList component to display an extensive list of menu items for the Little Lemon app.
The code below contains the list of menu items you want to display on the Little Lemon menu. It is
declared as an array of objects. A title categorizes each menu item. You have four categories:
appetizers, main dishes, sides, and desserts.
Within each category, there is a data array that contains the name of each menu item for that
category.
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
'Hummus',
'Moutabal',
'Falafel',
'Marinated Olives',
'Kofta',
'Eggplant Salad',
],
},
{
title: 'Main Dishes',
data: ['Lentil Burger', 'Smoked Salmon', 'Kofta Burger', 'Turkish Kebab'],
},
{
title: 'Sides',
data: [
'Fries',
'Buttered Rice',
'Bread Sticks',
'Pita Pocket',
'Lentil Soup',
'Greek Salad',
'Rice Pilaf',
],
},
{
title: 'Desserts',
data: ['Baklava', 'Tartufo', 'Tiramisu', 'Panna Cotta'],
},
];
To use the SectionList component, you will need to import it directly from the React Native
package since it is a
The SectionList component has two required props you will need to pass as a bare
minimum. It is very similar to the FlatList component you learned about in an earlier section
of the course.
The first prop is the sections that accept a plain array. This array contains the list of items to
display. The second required prop is the renderItem. The renderItem takes an item from
the sections and renders it into the list.
Now within the MenuItems component, you should render the SectionList component as
follows:
return (
<View style={menuStyles.container}>
<SectionList
keyExtractor={(item, index) => item + index}
sections={menuItemsToDisplay}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}
</SectionList>
</View>
);
};
Notice here that you have provided the sections prop with the name of the array you already
have defined, which is menuItemsToDisplay. The renderItem method calls a call-back
method which renders another component called Item. You can also see an additional prop, the
keyExtractor being passed to the SectionList component. It tells the list to use each id
as React keys.
Notice there is a renderSectionHeader call-back function passed to the prop as well. This is
used to render each section’s header. These examples would be Appetizers, Main Dishes and so
on. It renders title from the menuItemsToDisplay array.
In the above code, the Item component takes name as the prop and it renders name within a
parent view. The Item component is rendered for every item in the array until it reaches the end.
You can enhance your SectionList component with separators and footers if you want. They
are passed to the component via the ListFooterComponent prop and the
ItemSeparatorComponent prop as shown below:
return (
<View style={menuStyles.container}>
<SectionList
keyExtractor={(item, index) => item + index}
sections={menuItemsToDisplay}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}
ListFooterComponent={Footer}
ItemSeparatorComponent={Separator}></SectionList>
</View>
);
};
Below you'll find the component code for the separator and footer:
const Separator = () => <View style={menuStyles.separator} />;
The SectionList renders items lazily, which means it only renders items you visually see on
the screen, and once you start scrolling up or down, those items off the screen are removed, and
the new items are rendered. This form of lazy rendering is very performant and effective in
rendering large lists with sectional support in mobile apps.
Below are the styles used in this component for your reference:
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
'Hummus',
'Moutabal',
'Falafel',
'Marinated Olives',
'Kofta',
'Eggplant Salad',
],
},
{
title: 'Main Dishes',
data: ['Lentil Burger', 'Smoked Salmon', 'Kofta Burger', 'Turkish Kebab'],
},
{
title: 'Sides',
data: [
'Fries',
'Buttered Rice',
'Bread Sticks',
'Pita Pocket',
'Lentil Soup',
'Greek Salad',
'Rice Pilaf',
],
},
{
title: 'Desserts',
data: ['Baklava', 'Tartufo', 'Tiramisu', 'Panna Cotta'],
},
];
In this section, you learned how to use the SectionList component to render large lists with
section headers in React Native efficiently.
In this exercise, you will expand on the example from an earlier exercise to display a large list of
menu items with each item’s price. You will use the SectionList component within the Little
Lemon app.
By doing this, each menu item will be categorized according to its item type and will display section
headers such as Appetizers, Main dishes and so on.
Scenario
The Little Lemon app needs to display a large list of menu items on the screen, along with the price
of each item, sorted into sections. You have been asked to use the SectionList component to
render these menu items efficiently. Make sure that the component you write is readable and clean.
Below is the array of menu items, along with the price for each item
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
{ name: 'Hummus', price: '$5.00' },
{ name: 'Moutabal', price: '$5.00' },
{ name: 'Falafel', price: '$7.50' },
{ name: 'Marinated Olives', price: '$5.00' },
{ name: 'Kofta', price: '$5.00' },
{ name: 'Eggplant Salad', price: '$8.50' },
],
},
{
title: 'Main Dishes',
data: [
{ name: 'Lentil Burger', price: '$10.00' },
{ name: 'Smoked Salmon', price: '$14.00' },
{ name: 'Kofta Burger', price: '$11.00' },
{ name: 'Turkish Kebab', price: '$15.50' },
],
},
{
title: 'Sides',
data: [
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00' },
{ name: 'Bread Sticks', price: '$3.00' },
{ name: 'Pita Pocket', price: '$3.00' },
{ name: 'Lentil Soup', price: '$3.75' },
{ name: 'Greek Salad', price: '$6.00' },
{ name: 'Rice Pilaf', price: '$4.00' },
],
},
{
title: 'Desserts',
data: [
{ name: 'Baklava', price: '$3.00' },
{ name: 'Tartufo', price: '$3.00' },
{ name: 'Tiramisu', price: '$5.00' },
{ name: 'Panna Cotta', price: '$5.00' },
],
},
];
The screenshots below illustrate how your app should look after you complete this exercise:
The colors displayed in the images above can be applied using the following values:
#F4CE14, #EE9972, #333333, #EDEFEE, black and white.
Starter Code:
You can download the starter code for this exercise, from the zipped folder below:
Instructions
Step 1: Update MenuItems component to display new list of menu items and use
SectionList
To complete this exercise, you’ll first need to update the MenuItems component to display the
menu items and price.
Within this component, use the array provided in this scenario to pass to the data prop of the
SectionList component. Then configure the renderItem prop of the SectionList
component to render each item’s name as well as the price per item.
Hint: You can create multiple components within the same file to keep code clean.
In this step, you will use the SectionList component to render the section header. The
headers are provided within the title property in the array menuItemsToDisplay. Utilize it
to render the headers for each section of menu items. Make sure that each menu item is now
displayed inside the appropriate section.
In this step, you will style the new component that you have created to match the screenshots.
Make sure to provide meaningful names to all your styles.
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to configure
and utilize the SectionList component to render a large list of items by section.
MenuItems.js
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
{ name: 'Hummus', price: '$5.00' },
{ name: 'Moutabal', price: '$5.00' },
{ name: 'Falafel', price: '$7.50' },
{ name: 'Marinated Olives', price: '$5.00' },
{ name: 'Kofta', price: '$5.00' },
{ name: 'Eggplant Salad', price: '$8.50' },
],
},
{
title: 'Main Dishes',
data: [
{ name: 'Lentil Burger', price: '$10.00' },
{ name: 'Smoked Salmon', price: '$14.00' },
{ name: 'Kofta Burger', price: '$11.00' },
{ name: 'Turkish Kebab', price: '$15.50' },
],
},
{
title: 'Sides',
data: [
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00' },
{ name: 'Bread Sticks', price: '$3.00' },
{ name: 'Pita Pocket', price: '$3.00' },
{ name: 'Lentil Soup', price: '$3.75' },
{ name: 'Greek Salad', price: '$6.00' },
{ name: 'Rice Pilaf', price: '$4.00' },
],
},
{
title: 'Desserts',
data: [
{ name: 'Baklava', price: '$3.00' },
{ name: 'Tartufo', price: '$3.00' },
{ name: 'Tiramisu', price: '$5.00' },
{ name: 'Panna Cotta', price: '$5.00' },
],
},
];
return (
<View style={menuStyles.container}>
<SectionList
sections={menuItemsToDisplay}
keyExtractor={(item, index) => item + index}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}></SectionList>
</View>
);
};
Within the existing MenuItems component, render the SectionList component instead of
FlatList and pass to it the array from this scenario as shown below.
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
{ name: 'Hummus', price: '$5.00' },
{ name: 'Moutabal', price: '$5.00' },
{ name: 'Falafel', price: '$7.50' },
{ name: 'Marinated Olives', price: '$5.00' },
{ name: 'Kofta', price: '$5.00' },
{ name: 'Eggplant Salad', price: '$8.50' },
],
},
{
title: 'Main Dishes',
data: [
{ name: 'Lentil Burger', price: '$10.00' },
{ name: 'Smoked Salmon', price: '$14.00' },
{ name: 'Kofta Burger', price: '$11.00' },
{ name: 'Turkish Kebab', price: '$15.50' },
],
},
{
title: 'Sides',
data: [
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00' },
{ name: 'Bread Sticks', price: '$3.00' },
{ name: 'Pita Pocket', price: '$3.00' },
{ name: 'Lentil Soup', price: '$3.75' },
{ name: 'Greek Salad', price: '$6.00' },
{ name: 'Rice Pilaf', price: '$4.00' },
],
},
{
title: 'Desserts',
data: [
{ name: 'Baklava', price: '$3.00' },
{ name: 'Tartufo', price: '$3.00' },
{ name: 'Tiramisu', price: '$5.00' },
{ name: 'Panna Cotta', price: '$5.00' },
],
},
];
return (
<View>
<SectionList
sections={menuItemsToDisplay}
keyExtractor={(item, index) => item + index}
renderItem={renderItem}></SectionList>
</View>
);
};
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
{ name: 'Hummus', price: '$5.00' },
{ name: 'Moutabal', price: '$5.00' },
{ name: 'Falafel', price: '$7.50' },
{ name: 'Marinated Olives', price: '$5.00' },
{ name: 'Kofta', price: '$5.00' },
{ name: 'Eggplant Salad', price: '$8.50' },
],
},
{
title: 'Main Dishes',
data: [
{ name: 'Lentil Burger', price: '$10.00' },
{ name: 'Smoked Salmon', price: '$14.00' },
{ name: 'Kofta Burger', price: '$11.00' },
{ name: 'Turkish Kebab', price: '$15.50' },
],
},
{
title: 'Sides',
data: [
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00' },
{ name: 'Bread Sticks', price: '$3.00' },
{ name: 'Pita Pocket', price: '$3.00' },
{ name: 'Lentil Soup', price: '$3.75' },
{ name: 'Greek Salad', price: '$6.00' },
{ name: 'Rice Pilaf', price: '$4.00' },
],
},
{
title: 'Desserts',
data: [
{ name: 'Baklava', price: '$3.00' },
{ name: 'Tartufo', price: '$3.00' },
{ name: 'Tiramisu', price: '$5.00' },
{ name: 'Panna Cotta', price: '$5.00' },
],
},
];
return (
<View>
<SectionList
sections={menuItemsToDisplay}
keyExtractor={(item, index) => item + index}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}></SectionList>
</View>
);
};
Once you have coded a working component, it is time to style it. In this step, you will style the
MenuItems component to match the screenshots displayed above.
const menuItemsToDisplay = [
{
title: 'Appetizers',
data: [
{ name: 'Hummus', price: '$5.00' },
{ name: 'Moutabal', price: '$5.00' },
{ name: 'Falafel', price: '$7.50' },
{ name: 'Marinated Olives', price: '$5.00' },
{ name: 'Kofta', price: '$5.00' },
{ name: 'Eggplant Salad', price: '$8.50' },
],
},
{
title: 'Main Dishes',
data: [
{ name: 'Lentil Burger', price: '$10.00' },
{ name: 'Smoked Salmon', price: '$14.00' },
{ name: 'Kofta Burger', price: '$11.00' },
{ name: 'Turkish Kebab', price: '$15.50' },
],
},
{
title: 'Sides',
data: [
{ name: 'Fries', price: '$3.00', id: '11K' },
{ name: 'Buttered Rice', price: '$3.00' },
{ name: 'Bread Sticks', price: '$3.00' },
{ name: 'Pita Pocket', price: '$3.00' },
{ name: 'Lentil Soup', price: '$3.75' },
{ name: 'Greek Salad', price: '$6.00' },
{ name: 'Rice Pilaf', price: '$4.00' },
],
},
{
title: 'Desserts',
data: [
{ name: 'Baklava', price: '$3.00' },
{ name: 'Tartufo', price: '$3.00' },
{ name: 'Tiramisu', price: '$5.00' },
{ name: 'Panna Cotta', price: '$5.00' },
],
},
];
return (
<View style={menuStyles.container}>
<SectionList
sections={menuItemsToDisplay}
keyExtractor={(item, index) => item + index}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}></SectionList>
</View>
);
};
The feedback form will accept the user’s input via the virtual keyboard. To do this, React Native
provides the TextInput component out-of-the-box.
Import:
Let’s go ahead and import the TextInput component, as shown below, along with the other
necessary imports:
Note that you will have to import the useState hook as well. This will be used to keep track of the
local state of all the user inputs within the feedback form.
You will pass a style, value, and onChangeText prop to each text input. The value here
represents what the user is typing within the box, and it is the current value of the local state
variable of that box. The onChangeText prop will call back the set state method of each of the
local state variables.
For instance, for the first name, the onChangeText will get triggered as the user is typing, and
it will call the onChangeFirstName method and set the new or updated text to the
firstName variable.
<TextInput
style={styles.input}
value={firstName}
onChangeText={onChangeFirstName}
/>
<TextInput
style={styles.input}
value={lastName}
onChangeText={onChangeLastName}
/>
<TextInput
style={styles.messageInput}
value={message}
onChangeText={onChangeMessage}
/>
Similarly, you can also use the same pattern to set up the other text input boxes.
return (
<ScrollView style={styles.container}>
<Text style={styles.headingSection}>
How was your visit to Little Lemon?
</Text>
<Text style={styles.infoSection}>
Little Lemon is a charming neighborhood bistro that serves simple food
and classic cocktails in a lively but casual environment. We would love
to hear your experience with us!
</Text>
<TextInput
style={styles.input}
value={firstName}
onChangeText={onChangeFirstName}
/>
<TextInput
style={styles.input}
value={lastName}
onChangeText={onChangeLastName}
/>
<TextInput
style={styles.messageInput}
value={message}
onChangeText={onChangeMessage}
/>
</ScrollView>
);
};
In this reading you learned how to configure and use the TextInput component to build a
feedback form for the Little Lemon app.
In this exercise, you will create a sample text input box in the Little Lemon app that enters the user’s
name.
Scenario
For this exercise, your task is configuring a text input box on the Welcome screen, where the user
can enter their name. The input box should contain placeholder text, and the text that is input should
be stored in the local state.
Starter code
You can download the starter code from the zipped folder below:
The colors displayed in the images above can be applied using the following values:
Instructions
Step 1: Configure TextInput component on the Welcome Screen.
Your first step in this exercise is to configure the TextInput component on the Welcome
Screen. It will accept the user’s input.
Make sure to import the TextInput component from React Native and provide it with the
value and onChangeText props.
In this step, you will store what the user types within the component’s local state.
Hint: You’ll need to make use of the useState hook to accomplish this.
In this step, you will style the new component that you created to match the screenshots. Make
sure to provide meaningful names to all your styles.
Hint: Style the text input box by providing appropriate height, margin, padding, border width
and font size. You can play around with different styles here to get the desired outcome, so be
creative!
Conclusion
By completing this exercise, you will demonstrate your understanding and ability to configure
and utilize the TextInput component to accept user’s input within a text box.
The code block below outlines the outcome of the exercise in its entirety.
WelcomeScreen.js
The code above will produce the following output on the emulator:
Procedure
Next, review the breakdown of the individual steps below.
On the Welcome Screen, configure the TextInput component to accept user’s input.
<TextInput
value={firstName}
onChangeText={onChangeFirstName}
placeholder={'First Name'}
/>
In this step, make sure to import the useState hook from React and create a local state for
storing the user’s name that is being entered in the text input box.
Once you have coded a working component, it is time to style it. In this step, you will style the
TextInput component to match the screenshots above.
keyboardDismissMode
A common problem you will encounter while developing React Native apps is that the virtual
keyboard within a ScrollView will be constantly visible while you scroll through a page. You would
have to tap outside of the text input and dismiss it. This could get annoying to the user. Instead, you
can provide a seamless user experience by applying the keyboardDismissMode prop within your
ScrollView component.
Setting the prop value to on-drag will ensure the keyboard is dismissed when you start scrolling or
dragging on the screen. This will be seamless for the user, who can always tap back on the text
input box to open up the virtual keyboard again.
<ScrollView keyboardDismissMode="on-drag">
{/* Text inputs and other code ...
...
/*}
</ScrollView >
The keyboardDismissMode prop by default has the value none, which means the drags do not
dismiss the keyboard.
KeyboardAvoidingView
Another handy component for managing the virtual keyboard is the KeyboardAvoidingView.
As the name suggests, it is a component that automatically adjusts its height, position, or bottom
padding based on the keyboard’s height so that it remains visible. In contrast, the virtual
keyboard is still displayed. This component is part of the core react-native package and can
be imported directly as follows:
While using this component, you can set specific properties depending on whether it is an iOS or
Android device. To determine that, you can use the Platform API, also available within React
Native.
Configuring KeyboardAvoidingView
This component inherits all the props of View, making it essentially View with additional
capabilities. Within the KeyboardAvoidingView component, you can pass the behavior
prop. This prop determines how to react to the presence of the keyboard. Depending on the use
case, three values can be passed to it: height, position and padding.
In this example, you can see how the Platform API sets the behavior value to padding for
iOS devices and, height for all other devices.
<KeyboardAvoidingView
style={styles.container}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}>
...
/*}
</KeyboardAvoidingView>
return (
<KeyboardAvoidingView
style={styles.container}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}>
<ScrollView keyboardDismissMode="on-drag">
<Text style={styles.headingSection}>
How was your visit to Little Lemon?
</Text>
<Text style={styles.infoSection}>
Little Lemon is a charming neighborhood bistro that serves simple food
and classic cocktails in a lively but casual environment. We would
love to hear your experience with us!
</Text>
<TextInput
style={styles.input}
value={firstName}
onChangeText={onChangeFirstName}
/>
<TextInput
style={styles.input}
value={lastName}
onChangeText={onChangeLastName}
/>
<TextInput
style={styles.messageInput}
value={message}
onChangeText={onChangeMessage}
/>
</ScrollView>
</KeyboardAvoidingView>
);
};
In this reading, you explored some tips and tricks to handle the virtual keyboard in React Native
effectively.