📺 Develpreneur YouTube Episode

Video + transcript

React Native Fundamentals Part 11 - Tab Navigation

2021-10-28 •Youtube

Detailed Notes

In this video, we expand on React Navigation with Tab Navigation in React-Native. Specifically how to use Tabs to navigate around our screens instead of using the navigator.

Just in case you missed the last video you can find it here or you need a refresher you can find the first video Started Using React Native For Your Mobile Applications here.

Tab Navigation in React-Native focuses on: Required Dependencies Tabs IonIcons For a full list of components and APIs goto https://reactnative.dev/docs/accessibilityinfo

Tab Navigation

Possibly the most common style of navigation in mobile apps is tab-base navigation. Where the application displays tabs on the bottom of the screen or on the top below the header (or even instead of a header) for navigating between screens. Before we begin we need to install some additional dependencies for npm and expo.

Installation and setup

First, you need to install them in your project:

npm install @react-navigation/native React Tab Navigation is made up of some core utilities we use to create the navigation structures in our apps. These utilities include with @react-navigation/native are:

@react-navigation/bottom-tabs @react-navigation/stack @expo/vector-icons/Ionicons react-native-vector-icons/Ionicons Using the Stack Navigator With Tabs

Usually tabs don't just display one screen; For example, on your Facebook feed, you can tap on a post and it brings you to a new screen within that tab with all of the comments. Another way to think about would be to think of this as being a separate navigation stacks within each tab, and that's exactly how we will model it in React Navigation.

Customizing Tabs

This is similar to how you would customize a stack navigator. While there are some properties that get set when you initialize the tab navigator and others you customize per-screen in the options. One such option is using Ionicons.

Ionicons

Premium designed icons for use in web, iOS, Android, and desktop apps. Support for SVG and web font. Completely open source, MIT licensed and built by the Ionic Framework team.

Additional Resources React - https://reactjs.org/ Node - https://nodejs.org/en/ Visual Studio Code - https://code.visualstudio.com/ Android Development - https://developer.android.com/studio Xcode - https://apps.apple.com/us/app/xcode/id497799835?mt=12

Transcript Text
[Music]
so today
so welcome today's presentation we're
going to continue talking about react
native
and this
presentation is going to be a
continuation on our discussion about
navigation with react native we're
basically
how to add navigation to our mobile
applications web applications or however
we're wanting to interact or move
between pages within an app
so last time we talked about using
react navigation and how we could use
the stack and the stack navigator to
actually add some screens to a nice
stack flow so that we could interact and
jump back and forth between an
application
so for that we had to install some
dependencies for
node and
expo those were the react navigation
native
dependency and the stack dependency
we also included some optional
dependencies like the reanimated the
gesture handlers
the screens the safe area content and
the mask view now for today's
presentation we're talking about tabs
we're going to need the native and the
stack but we don't need the optional
dependencies
we'll also need one additional
dependency and i'll talk about that
later as we go through the presentation
today
the bottom section here is essentially
the
libraries that we had to import within
our project and the new stack container
and stack navigator and stack screen
tags that we used for customizing our
application to include screens
and button navigations to go back and
forth between
the screen
so today we're getting into tabs
and as the definition on react native
it's possibly the most commonly style of
navigation in mobile apps is tab
navigation
now tabs can be on the bottom of the
screen or they can be on the top of the
screen
however this is
kind of dependent upon the type of
device you're using so if you're using
ios
apple products all the navigation or tab
navigation is typically defined at the
bottom of the screen now when you're
dealing with android most of your
navigation is at the top of the screen
because they typically have button
navigations at the bottom of the
physical device that you can use to go
back and forward between your
applications
so the requirements that we need to
include today is we need to make sure
that we use the native
uh component that we installed the react
navigation native for our project
and we also include the react navigation
bottom tabs
now i know i mentioned before
that
depending upon your application or
depending on your device your tabs could
be at the top or the bottom well with
react native the library itself is just
bottom tabs and the bottom tabs are
going to be
it's just a library for tabbing when you
run react native on your device the tabs
are going to conform to the nature of
the device so if you're on ios it'll be
at the bottom if you're on android it'll
be at the top so you don't have to
actually customize the location of your
tabs
and then when we create our applications
we need to make sure that we import the
navigation container like we did before
for the react navigation so that we have
the native
uh components
and we also want to import the create
bottom tab navigator from the bottom
tabs libraries
all right so let's get started
all right so the first thing we want to
do is we want to install
a new application or we want to create a
new application so we're still using
expo so we'll use expo install
or sorry expo inet
and here we're going to create a new
application called sample
app six since this is our sixth
presentation on react native
and it will take it a second to get the
libraries and
prompt you to
as to what template you want to use
so to start with even though there is an
example here with tab
for now we're going to start with just a
blank application
take this a second to install
my
there we go
all right so now this has created a new
project so to access our project it
tells us how to start so we're going to
go cd
sample f6
and if we look in here we have all of
our react native
uh code or retina project that we need
now because we're using expo and we need
to
use or we're going to add tabs and
navigation we need to also install
those libraries
so i'm going to go to my little cheat
sheet here
it is
and i need to install all of this
so we react native
and i need
now the reason we have to install these
every time we create a project is it
installs the libraries for these
components into our project
that way when we run our application in
expo
uh it will actually load these
dependencies to the mobile application
as well
okay now the next thing we want to do is
we want to start expo so we will do expo
start
another tab here and i'm actually going
to open up the code and visual code
all right expo started
let's
select our ios
device let's do
iphone 8
and there's our iphone 8 emulator so i'm
going to minimize
the console we don't need that
put now guy over here
all right so the first thing we need to
do is we need to open up our app.json
oops sorry
app.js here we go
upload for a second one moment here
all right so as you can see when the
application loaded we have our input
status bar which is what we would expect
for
using expo
we always have to import or react
because react native
is built on top of react
and then we also want to import some
basic modules like style sheet text and
view
now the other thing we need to use for
react navigation
and for our tabs is we're going to have
to import a few more libraries here so
we'll import application
container
from
react navigate
all right and then we also need to
import
create
one tab
navigation navigate tor
from
react
location
bottom
tap
now if i save that it's going to fail
because i have i just remembered i did
not install the react navigation bottom
tab what's cool here is it tells you
that
your error message is pretty distinct it
tells you oops
we cannot find this library within our
project so to fix that
you go out to your console
you stop the server
and
copy this guy right here
and do an expo install
that one we want
tabs yeah
and don't mind these package errors
i'm finding that having the new
uh macbook pro with the m1 chip it's
giving
me some fits with some libraries but uh
overall everything does work
all right so we're gonna restart expo
and then once that starts we're going to
reload
our app
grab this reloaded and it's bundling up
all those javascript libraries including
the new one we just installed so now our
app actually loads
all right so we've created a new project
we have imported our navigation
container and we've imported our create
bottom tab navigation or navigator
right so now we need some screen we have
our base screen here that is defined
inside of our app but we don't want to
hard code our screen in our app we want
to actually
pull those components out into separate
functions
i'll create a new function
and call this one screen one
and we'll create another one
screen two
all right so screen one we're going to
return
and inside our return we're going to
create a new view
actually i'm just going to cheat i'm
going to copy this view here to start
with
all right so we have our view
we're going to use our styles instead of
container i'm going to create a screen
style here
and down in my styles we'll add screen
and he'll be flex one
and we'll keep the alignments the same
but we will drop the default
color
and justify
yep okay
so we have our screen one
and our text let's just
label
or print out
okay we're in screen one
all right we don't need the status bar
so we'll get rid of that guy
and let's just copy this code for screen
two and change it to screen two
all right so we've created two functions
or two screens
uh or two views i'm sorry so view one is
going to give us a
view with some text called screen one
and function two or function screen two
is gonna return us another view with
screen two
so right now we just have two components
here with two views that aren't really
attached to the application yet
so before we can actually attach them we
need to add a constant
and let's call it tab
this way we can now reference the create
bottom tab navigator
by using the tab constant so instead of
having to type this every single time we
can now just call tab
so now let's go into our app here
remove this view
start fresh
so now let's look at our
code so before we had to create a
navigation container so we still need to
do that
we'll come back over here so the first
thing we need to do is add our
navigation container
and inside our navigation container we
now need to add our tab navigator so to
do that we're going to use our tab
constant
and then we're going to do dot tab
tab dot
and we're going to go find
screen
now the tab stream
is a self-closing tab
with a couple parameters
for attributes it has the name attribute
and it has the component
attribute
now name is going to be our unique
identifier which is how we're going to
reference this screen or this tab
throughout the application so this one
we'll call it screen1
and we'll add the component
being our function here of screen one
and we'll do this again for screen 2.
now if i save this
so i forgot one thing
all right so we have our
navigation container we need to add our
it's not navigation container we need
the tab
dot
navigation container
and oh yeah and i need the application
hey there
oh
oh
oops
there it is
sorry about that sometimes the error
messages are not super clear on what it
is that's wrong i forgot the
open close parenthesis to load the
constructor for this
component there we go
you
Transcript Segments
0.42

[Music]

27.039

so today

28.88

so welcome today's presentation we're

31.119

going to continue talking about react

32.719

native

33.68

and this

35.2

presentation is going to be a

36.32

continuation on our discussion about

38.48

navigation with react native we're

40.96

basically

41.92

how to add navigation to our mobile

44.239

applications web applications or however

46.96

we're wanting to interact or move

48.8

between pages within an app

51.68

so last time we talked about using

55.44

react navigation and how we could use

58.399

the stack and the stack navigator to

61.6

actually add some screens to a nice

64.559

stack flow so that we could interact and

66.479

jump back and forth between an

67.92

application

69.04

so for that we had to install some

71.52

dependencies for

73.6

node and

74.88

expo those were the react navigation

77.92

native

78.88

dependency and the stack dependency

82.479

we also included some optional

84.159

dependencies like the reanimated the

86.24

gesture handlers

88

the screens the safe area content and

90.64

the mask view now for today's

93.119

presentation we're talking about tabs

95.2

we're going to need the native and the

96.96

stack but we don't need the optional

98.96

dependencies

100.72

we'll also need one additional

102.399

dependency and i'll talk about that

103.759

later as we go through the presentation

105.759

today

106.88

the bottom section here is essentially

109.119

the

109.84

libraries that we had to import within

111.68

our project and the new stack container

115.04

and stack navigator and stack screen

117.439

tags that we used for customizing our

120.799

application to include screens

123.28

and button navigations to go back and

125.119

forth between

126.479

the screen

130.399

so today we're getting into tabs

132.8

and as the definition on react native

135.68

it's possibly the most commonly style of

138

navigation in mobile apps is tab

141.36

navigation

142.56

now tabs can be on the bottom of the

144.48

screen or they can be on the top of the

147.04

screen

148

however this is

150.319

kind of dependent upon the type of

152.319

device you're using so if you're using

155.2

ios

157.04

apple products all the navigation or tab

159.599

navigation is typically defined at the

162.4

bottom of the screen now when you're

164.4

dealing with android most of your

166.56

navigation is at the top of the screen

168.879

because they typically have button

170.64

navigations at the bottom of the

172.959

physical device that you can use to go

175.12

back and forward between your

176.48

applications

179.76

so the requirements that we need to

181.599

include today is we need to make sure

183.84

that we use the native

186

uh component that we installed the react

188.64

navigation native for our project

191.519

and we also include the react navigation

194.319

bottom tabs

203.2

now i know i mentioned before

206.159

that

210.4

depending upon your application or

212.48

depending on your device your tabs could

215.12

be at the top or the bottom well with

217.68

react native the library itself is just

220.239

bottom tabs and the bottom tabs are

223.04

going to be

224.48

it's just a library for tabbing when you

228.08

run react native on your device the tabs

231.28

are going to conform to the nature of

234.08

the device so if you're on ios it'll be

236.08

at the bottom if you're on android it'll

238.319

be at the top so you don't have to

240.08

actually customize the location of your

242.959

tabs

245.439

and then when we create our applications

247.439

we need to make sure that we import the

249.599

navigation container like we did before

252

for the react navigation so that we have

254.08

the native

255.12

uh components

256.639

and we also want to import the create

259.359

bottom tab navigator from the bottom

261.84

tabs libraries

265.36

all right so let's get started

267.44

all right so the first thing we want to

268.96

do is we want to install

271.44

a new application or we want to create a

273.28

new application so we're still using

275.6

expo so we'll use expo install

278.8

or sorry expo inet

281.28

and here we're going to create a new

284.4

application called sample

286.479

app six since this is our sixth

289.199

presentation on react native

292.96

and it will take it a second to get the

295.28

libraries and

296.88

prompt you to

298.88

as to what template you want to use

301.28

so to start with even though there is an

304.16

example here with tab

307.039

for now we're going to start with just a

308.639

blank application

312.56

take this a second to install

348.8

my

354.32

there we go

356.639

all right so now this has created a new

358.639

project so to access our project it

361.199

tells us how to start so we're going to

362.72

go cd

368.72

sample f6

371.12

and if we look in here we have all of

373.199

our react native

375.36

uh code or retina project that we need

378.88

now because we're using expo and we need

382.08

to

383.44

use or we're going to add tabs and

385.12

navigation we need to also install

388.16

those libraries

389.68

so i'm going to go to my little cheat

391.52

sheet here

396.479

it is

398.319

and i need to install all of this

417.039

so we react native

430.8

and i need

450.319

now the reason we have to install these

452.319

every time we create a project is it

454.639

installs the libraries for these

457.759

components into our project

464.4

that way when we run our application in

467.28

expo

468.4

uh it will actually load these

469.84

dependencies to the mobile application

472

as well

473.68

okay now the next thing we want to do is

475.52

we want to start expo so we will do expo

480.879

start

486.96

another tab here and i'm actually going

488.879

to open up the code and visual code

495.44

all right expo started

498.8

let's

500.8

select our ios

503.84

device let's do

505.68

iphone 8

509.36

and there's our iphone 8 emulator so i'm

511.599

going to minimize

513.44

the console we don't need that

519.599

put now guy over here

530.959

all right so the first thing we need to

532.32

do is we need to open up our app.json

537.92

oops sorry

539.36

app.js here we go

544.88

upload for a second one moment here

577.279

all right so as you can see when the

579.36

application loaded we have our input

581.44

status bar which is what we would expect

583.839

for

584.8

using expo

586.399

we always have to import or react

589.04

because react native

590.399

is built on top of react

592.88

and then we also want to import some

595.279

basic modules like style sheet text and

598.64

view

600.64

now the other thing we need to use for

603.76

react navigation

606

and for our tabs is we're going to have

608.16

to import a few more libraries here so

609.76

we'll import application

613.519

container

617.04

from

620.56

react navigate

629.36

all right and then we also need to

631.2

import

635.6

create

638.399

one tab

640.399

navigation navigate tor

645.12

from

648.32

react

652

location

655.04

bottom

657.12

tap

660.959

now if i save that it's going to fail

663.68

because i have i just remembered i did

666.24

not install the react navigation bottom

668.88

tab what's cool here is it tells you

671.519

that

672.32

your error message is pretty distinct it

674.079

tells you oops

675.76

we cannot find this library within our

678.16

project so to fix that

680.48

you go out to your console

683.6

you stop the server

687.68

and

688.72

copy this guy right here

692.079

and do an expo install

699.68

that one we want

702.88

tabs yeah

710.88

and don't mind these package errors

714.56

i'm finding that having the new

716.959

uh macbook pro with the m1 chip it's

720.079

giving

721.12

me some fits with some libraries but uh

723.92

overall everything does work

726.8

all right so we're gonna restart expo

734.72

and then once that starts we're going to

737.6

reload

740.72

our app

742.24

grab this reloaded and it's bundling up

745.12

all those javascript libraries including

747.6

the new one we just installed so now our

749.6

app actually loads

753.519

all right so we've created a new project

756.24

we have imported our navigation

758.24

container and we've imported our create

761.12

bottom tab navigation or navigator

764.959

right so now we need some screen we have

768.079

our base screen here that is defined

770.399

inside of our app but we don't want to

773.12

hard code our screen in our app we want

776

to actually

778

pull those components out into separate

780.48

functions

781.76

i'll create a new function

785.12

and call this one screen one

790.56

and we'll create another one

796.56

screen two

798.079

all right so screen one we're going to

800.639

return

808.399

and inside our return we're going to

810.56

create a new view

812.48

actually i'm just going to cheat i'm

814.16

going to copy this view here to start

815.839

with

818.079

all right so we have our view

819.92

we're going to use our styles instead of

822.16

container i'm going to create a screen

824.399

style here

827.76

and down in my styles we'll add screen

831.839

and he'll be flex one

834.48

and we'll keep the alignments the same

836.72

but we will drop the default

840.839

color

842.56

and justify

845.199

yep okay

848.079

so we have our screen one

850.32

and our text let's just

852.32

label

853.279

or print out

854.639

okay we're in screen one

856.8

all right we don't need the status bar

859.199

so we'll get rid of that guy

861.519

and let's just copy this code for screen

864.24

two and change it to screen two

873.12

all right so we've created two functions

875.36

or two screens

877.279

uh or two views i'm sorry so view one is

881.44

going to give us a

883.6

view with some text called screen one

886.72

and function two or function screen two

889.36

is gonna return us another view with

891.199

screen two

892.72

so right now we just have two components

894.639

here with two views that aren't really

896.959

attached to the application yet

900.88

so before we can actually attach them we

903.68

need to add a constant

909.12

and let's call it tab

911.68

this way we can now reference the create

915.519

bottom tab navigator

917.44

by using the tab constant so instead of

920

having to type this every single time we

922.079

can now just call tab

924.72

so now let's go into our app here

927.04

remove this view

930.48

start fresh

932.24

so now let's look at our

937.04

code so before we had to create a

939.04

navigation container so we still need to

941.519

do that

943.44

we'll come back over here so the first

945.279

thing we need to do is add our

946.8

navigation container

951.6

and inside our navigation container we

954

now need to add our tab navigator so to

956.959

do that we're going to use our tab

959.839

constant

961.759

and then we're going to do dot tab

964.16

tab dot

966.56

and we're going to go find

968.399

screen

969.759

now the tab stream

971.839

is a self-closing tab

975.6

with a couple parameters

977.92

for attributes it has the name attribute

981.519

and it has the component

984.24

attribute

987.04

now name is going to be our unique

989.6

identifier which is how we're going to

991.36

reference this screen or this tab

994.24

throughout the application so this one

996.399

we'll call it screen1

1000.079

and we'll add the component

1002.88

being our function here of screen one

1008.8

and we'll do this again for screen 2.

1011.759

now if i save this

1014

so i forgot one thing

1016.24

all right so we have our

1018.16

navigation container we need to add our

1022.959

it's not navigation container we need

1024.72

the tab

1026.24

dot

1027.439

navigation container

1029.439

and oh yeah and i need the application

1033.839

hey there

1039.199

oh

1040.16

oh

1041.28

oops

1042.64

there it is

1045.6

sorry about that sometimes the error

1047.52

messages are not super clear on what it

1050.64

is that's wrong i forgot the

1054.72

open close parenthesis to load the

1057.52

constructor for this

1059.6

component there we go

1076.96

you