📺 Develpreneur YouTube Episode

Video + transcript

React Native Fundamentals - Part 14 - Drawer Navigation Part 3

2021-11-09 •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]
right so the third example we're going
to look at is we're now going to mix the
navigations using that stack object so
we're going to use
that's that
we're going to add a stack
and what we're going to do here is we're
instead of adding home and notifications
directly to the drawer we're going to
add home
and notifications to a stack
and then add that stack to our drawer
don't need button view
but i need to add another library
so we need to import
create
stack
but it's not seeing that is not making
me happy
it means that we may have to
[Music]
reinstall that library
okay there we go all right so we have
our create stack navigator
so just like we did here for the drawer
we need to create a constant
or stack
and set it equal to our create stack
navigator
and essentially call the constructor
so now we can use whoops
so now we can use stack
in our code
it didn't fail so so far so good
all right so stack is going to be almost
identical to drawer except in this case
because we are creating stack as a
container essentially to stick onto our
drawer navigator we don't need another
navigation container we just need to
create a stack navigator
so here we're going to create a new
function
and we're going to call this one home
stack
and home stack is going to have a return
and we need to create a stack
or use our stack object
dot
navigator
so just like drawer navigator
so this is our wrapper for our stack
right here
and then we need to add the screens to
the stack so just like drawer we do
stack
screen
and just like the drawer we need the
name
we will give the alias which is what
we're going to use within this navigator
and we will give it the home screen
and we close
the screen and then we will duplicate
this for notification
all right so now we have our stack
navigator
currently we are not applying it here
because if we click the button here we
are still going back and forth
right so now we have our stack navigator
so we now need to take this stack
and place it into our navigation
container
it we'll replace the home screen with
home stack
and we will get rid of notifications
if we save this
the first thing you will notice is that
we lose notifications from our menu
which is fine
because what we're saying here is that
notifications really applies to the home
stats so you really want to navigate to
notifications only from home
and so to do that we now have home we
click go to notifications we now get
this back button up here which is
symbolic with the stack so we're
basically saying that uh within the home
it's in a stack of navigation so we're
now pushing and popping
the stack
into the window here so every time we
click go to notifications we are pushing
notifications to the top of the stack
and when i click home to go back i am
pulling notifications back on the stack
whereas if i click the drawer
i'm actually replacing the window or the
screen here completely it is
i'm using the drawer stack
so there's no internal navigation
now
since home does have a stack when i
click home
it moves back and forth within the stack
okay any questions on mixing of the
navigations using stack
nope
all right so the last example i want to
show you is adding a menu button so when
we look at our application it's not
really intuitive that we have a drawer
hiding over here or some kind of menu
off screen
so let's add a menu button to the header
of our screens
so in order to add a header we actually
need to wrap
all of our screens in some type of stack
so right now
you can see here we have home up here
because our home stack
contains home and notifications by doing
that by default it will put the name of
the screen
at the top so it gives us a title
bar
with some type of navigation
for navigating our screens
however if we look at screens
for settings
you notice we don't have that title bar
so we don't have that button navigation
at the top of our screen
so the first thing we want to do is we
want to create a
stack wrapper
for settings
and all it's going to contain is
settings
and then we want to replace
our settings screen here in our drawer
with the
setting stack
but now you see we have our settings
title
and what you don't see currently is we
don't have any navigation we have no
back or forward buttons up here but they
are there we just have to configure them
so for that
we need to import a few things so the
first thing we need to import here is
now we want the style sheet so now we
want to add our style
the
component
because now when we add the menu buttons
it's going to get a little ugly if we
just copy and pasted styles all over the
place
we still need our navigation we still
need our drawer and we still need our
stack
so the first thing i want to do
is come down to my stack
so here we are we have our home stack
all right so in our home stack here
under our screen so let's add some
we have our name
property we have our component property
so to
modify or change the title here we now
need to add some things to the options
property
of the screen
and then we will create a wrapper
object
and inside
of our object
we're going to have a list of properties
starting with header
title
so now we will specifically state what
we want to call
our screen go home
comma
and then we're going to use the header
left
property
to define the left of navigation what do
we put here for our left navigation
and since we want to actually add a
button we are going to create an object
and then we are going to essentially
call or inject a button
oh yeah
parentheses
oh yeah not
sorry wrong
tag that
okay sorry about that
squigglies and braces get confusing
after a while all right so we have our
header left
option
we are
defining an object
and now let's add our code
so inside of here we're going to add a
button object
i'm going to add an on press
and on press we're going to create an
inline call
to our navigation
dot
open
drawer
so this is a new
function we're calling so where we did
navigate
here to navigate to a screen
navigation open drawer is going to open
up our drawer here it's going to trigger
the open command when we
slide over
so open drawer
opens our drawer
and then let's give it a title so that
we can actually
know what this button is
and we'll call it menu
okay
not done yet
oh yeah
let's see what's going on
property there we go
and now if i go back to home oh look we
now have a menu button if i click the
menu button oops failed something happen
now what's happened and i purposely did
this is
see this navigation object here
well inside of this function we don't
have a navigator we don't have our
navigation container so in order for us
to access this container we have to
actually pass in the navigation object
to that we just add it to our function
call
and it will now pull in the navigation
so now when i click the menu button it
opens up our screen
so let's do this again for the other
components
notifications
notifications
we want it unsent
now can anyone tell me why i would not
want it on notification
so why might i not want to put our menu
button on our notifications screen
you don't want to put a menu
right so why would i only want to put
the menu
on home
and on settings why would i not want to
put it on notification
oh sure that's something i would say
maybe uh because you know uh you won't
be able to view your notifications
uh so that they're not under a menu
pretty much
so because of how we have defined our
application we have wrap
so we've created a stack
for home
so home is actually the top screen of
our stack here so any screens that we
add inside of this stack navigator
should all really reference back to
home or the home screen
not the menu
so essentially anything that you want to
add a menu button to on the individual
screens should be the same screen that
shows up in the drawer
if i click home i should have menu but
if i go to notifications what i should
have for my navigation is return to home
not menu
go to settings
settings again
[Music]
change my screen name
there we go i'm on settings
so i can go to home this way i can go to
notifications i can go back to home
and go back to home i can click the menu
there's home
menu and settings
so
you can combine multiple
navigation components within an
application you just have to think
through how you want to attach your
screens to that form of navigation
if you are going to have a menu
if you're going to have what i did i
accidentally
that happens sometimes you'll
accidentally hit a key save it looks
like it's working
but you accidentally broke something
functions home screen button title
and under settings we will add those
here
oh
can anyone see what i did wrong here
why did copying this code in here break
this screen
the answer is right here we can't find
the variable navigation
so while i copied and pasted the code i
forgot to inject the navigation object
from
the navigation container
now by adding it the navigation call
here to open drawer works
so now the menu call works
all right any questions on this
example so let's quickly review so we
covered the different required
dependencies for
using react
navigative navigation and that's a
tongue twister
in order for us to actually include
things in our applications we need to
make sure that we've installed the react
navigation library for navigate we need
to include the drawer library and if we
want to combine different components we
need to also include things like the
stack button
and any other components that we need
for our applications
as always we'd like to thank you for
your time
if you would like to discuss any of
these topics further or if you would
like to give us ideas for additional
topics to talk about you can reach us at
our email address at info developer.com
you can find us on our website at
developernerd.com contact us you can
find us on youtube by googling
developerner
space youtube you can also find us on
vimeo at vimeo.com developer nerd our
goal is making every developer better
thank you and have a great day
you
Transcript Segments
1.32

[Music]

26.72

right so the third example we're going

28.88

to look at is we're now going to mix the

31.119

navigations using that stack object so

34.64

we're going to use

36.079

that's that

38.559

we're going to add a stack

40.719

and what we're going to do here is we're

43.12

instead of adding home and notifications

45.76

directly to the drawer we're going to

48

add home

49.44

and notifications to a stack

52.239

and then add that stack to our drawer

63.76

don't need button view

65.36

but i need to add another library

68.64

so we need to import

75.2

create

78.08

stack

94.72

but it's not seeing that is not making

97.2

me happy

99.52

it means that we may have to

101.55

[Music]

102.88

reinstall that library

112.24

okay there we go all right so we have

114.479

our create stack navigator

117.759

so just like we did here for the drawer

120.159

we need to create a constant

124.479

or stack

126.88

and set it equal to our create stack

129.44

navigator

131.12

and essentially call the constructor

133.92

so now we can use whoops

136.319

so now we can use stack

138.48

in our code

143.36

it didn't fail so so far so good

147.28

all right so stack is going to be almost

149.599

identical to drawer except in this case

153.12

because we are creating stack as a

156.48

container essentially to stick onto our

159.04

drawer navigator we don't need another

161.28

navigation container we just need to

163.599

create a stack navigator

165.92

so here we're going to create a new

168

function

171.68

and we're going to call this one home

173.2

stack

181.92

and home stack is going to have a return

189.12

and we need to create a stack

192.159

or use our stack object

195.2

dot

196.08

navigator

199.2

so just like drawer navigator

201.36

so this is our wrapper for our stack

203.92

right here

205.28

and then we need to add the screens to

206.959

the stack so just like drawer we do

208.959

stack

214.08

screen

216.319

and just like the drawer we need the

218.56

name

220.959

we will give the alias which is what

222.959

we're going to use within this navigator

235.36

and we will give it the home screen

242.159

and we close

244.799

the screen and then we will duplicate

246.959

this for notification

261.12

all right so now we have our stack

262.56

navigator

263.68

currently we are not applying it here

266.24

because if we click the button here we

267.919

are still going back and forth

270.56

right so now we have our stack navigator

273.04

so we now need to take this stack

276

and place it into our navigation

278.32

container

280.8

it we'll replace the home screen with

282.479

home stack

283.84

and we will get rid of notifications

288.08

if we save this

290.08

the first thing you will notice is that

292.16

we lose notifications from our menu

295.84

which is fine

297.199

because what we're saying here is that

299.52

notifications really applies to the home

302.479

stats so you really want to navigate to

304.88

notifications only from home

307.759

and so to do that we now have home we

310.32

click go to notifications we now get

312.88

this back button up here which is

315.44

symbolic with the stack so we're

317.6

basically saying that uh within the home

321.6

it's in a stack of navigation so we're

323.52

now pushing and popping

326.88

the stack

329.6

into the window here so every time we

332.88

click go to notifications we are pushing

337.12

notifications to the top of the stack

339.52

and when i click home to go back i am

341.68

pulling notifications back on the stack

346.16

whereas if i click the drawer

348.88

i'm actually replacing the window or the

351.6

screen here completely it is

354.56

i'm using the drawer stack

357.199

so there's no internal navigation

359.6

now

360.639

since home does have a stack when i

362.56

click home

363.6

it moves back and forth within the stack

370.479

okay any questions on mixing of the

374

navigations using stack

379.28

nope

381.199

all right so the last example i want to

383.759

show you is adding a menu button so when

386.8

we look at our application it's not

388.8

really intuitive that we have a drawer

390.88

hiding over here or some kind of menu

392.96

off screen

396.479

so let's add a menu button to the header

398.88

of our screens

400.72

so in order to add a header we actually

404.16

need to wrap

406.24

all of our screens in some type of stack

409.68

so right now

411.759

you can see here we have home up here

413.68

because our home stack

416.56

contains home and notifications by doing

419.599

that by default it will put the name of

422.96

the screen

424.24

at the top so it gives us a title

426.88

bar

427.759

with some type of navigation

430

for navigating our screens

432.24

however if we look at screens

436.72

for settings

438.08

you notice we don't have that title bar

440

so we don't have that button navigation

441.919

at the top of our screen

443.599

so the first thing we want to do is we

445.36

want to create a

447.039

stack wrapper

448.639

for settings

458.4

and all it's going to contain is

460.24

settings

469.759

and then we want to replace

473.84

our settings screen here in our drawer

476.479

with the

484.639

setting stack

490.639

but now you see we have our settings

492.72

title

494.24

and what you don't see currently is we

496.24

don't have any navigation we have no

498.4

back or forward buttons up here but they

501.039

are there we just have to configure them

507.759

so for that

509.919

we need to import a few things so the

512.959

first thing we need to import here is

514.88

now we want the style sheet so now we

517.36

want to add our style

520.959

the

522

component

523.68

because now when we add the menu buttons

525.6

it's going to get a little ugly if we

527.519

just copy and pasted styles all over the

529.68

place

531.12

we still need our navigation we still

533.279

need our drawer and we still need our

535.68

stack

539.76

so the first thing i want to do

542

is come down to my stack

545.92

so here we are we have our home stack

550.48

all right so in our home stack here

554.32

under our screen so let's add some

558.08

we have our name

560.399

property we have our component property

563.36

so to

565.36

modify or change the title here we now

568.959

need to add some things to the options

571.519

property

573.04

of the screen

577.2

and then we will create a wrapper

580.48

object

582.399

and inside

584.16

of our object

586.08

we're going to have a list of properties

588.48

starting with header

592.24

title

594.72

so now we will specifically state what

596.959

we want to call

599.12

our screen go home

602.32

comma

604.079

and then we're going to use the header

606.88

left

609.839

property

612.8

to define the left of navigation what do

617.12

we put here for our left navigation

620.64

and since we want to actually add a

623.12

button we are going to create an object

626.56

and then we are going to essentially

628.8

call or inject a button

643.76

oh yeah

647.76

parentheses

650.88

oh yeah not

652.64

sorry wrong

654.079

tag that

667.279

okay sorry about that

670.32

squigglies and braces get confusing

672.72

after a while all right so we have our

674.64

header left

676.88

option

678.32

we are

679.6

defining an object

683.44

and now let's add our code

686.32

so inside of here we're going to add a

688.56

button object

691.68

i'm going to add an on press

700.56

and on press we're going to create an

702.88

inline call

705.44

to our navigation

708.8

dot

709.92

open

713.68

drawer

718.48

so this is a new

720.079

function we're calling so where we did

722.399

navigate

725.519

here to navigate to a screen

728.839

navigation open drawer is going to open

731.92

up our drawer here it's going to trigger

733.68

the open command when we

736.24

slide over

737.92

so open drawer

739.44

opens our drawer

744.959

and then let's give it a title so that

747.6

we can actually

749.2

know what this button is

753.12

and we'll call it menu

761.68

okay

762.639

not done yet

766.32

oh yeah

767.92

let's see what's going on

770.839

property there we go

776.24

and now if i go back to home oh look we

778.32

now have a menu button if i click the

780.48

menu button oops failed something happen

784.24

now what's happened and i purposely did

786.88

this is

788.72

see this navigation object here

792

well inside of this function we don't

795.12

have a navigator we don't have our

797.92

navigation container so in order for us

801.04

to access this container we have to

803.68

actually pass in the navigation object

807.76

to that we just add it to our function

809.76

call

812.16

and it will now pull in the navigation

815.519

so now when i click the menu button it

817.519

opens up our screen

820.399

so let's do this again for the other

824.399

components

839.279

notifications

853.92

notifications

862.56

we want it unsent

864.399

now can anyone tell me why i would not

866.72

want it on notification

884.88

so why might i not want to put our menu

887.68

button on our notifications screen

898

you don't want to put a menu

900.88

right so why would i only want to put

902.959

the menu

905.04

on home

906.399

and on settings why would i not want to

908.24

put it on notification

910.959

oh sure that's something i would say

913.12

maybe uh because you know uh you won't

915.279

be able to view your notifications

917.519

uh so that they're not under a menu

923.519

pretty much

924.56

so because of how we have defined our

927.199

application we have wrap

930.24

so we've created a stack

932.399

for home

933.759

so home is actually the top screen of

936.88

our stack here so any screens that we

939.519

add inside of this stack navigator

941.92

should all really reference back to

944.399

home or the home screen

946.72

not the menu

949.6

so essentially anything that you want to

951.839

add a menu button to on the individual

954.399

screens should be the same screen that

957.12

shows up in the drawer

963.759

if i click home i should have menu but

966.56

if i go to notifications what i should

968.959

have for my navigation is return to home

971.44

not menu

976.48

go to settings

978

settings again

981.68

[Music]

984.959

change my screen name

990

there we go i'm on settings

992.16

so i can go to home this way i can go to

994.32

notifications i can go back to home

997.519

and go back to home i can click the menu

999.839

there's home

1000.959

menu and settings

1003.44

so

1004.24

you can combine multiple

1007.68

navigation components within an

1010.079

application you just have to think

1012.32

through how you want to attach your

1014.639

screens to that form of navigation

1017.36

if you are going to have a menu

1023.759

if you're going to have what i did i

1025.919

accidentally

1033.6

that happens sometimes you'll

1036.4

accidentally hit a key save it looks

1039.28

like it's working

1040.64

but you accidentally broke something

1045.919

functions home screen button title

1062.88

and under settings we will add those

1066.48

here

1078.96

oh

1080.48

can anyone see what i did wrong here

1086.16

why did copying this code in here break

1089.12

this screen

1091.76

the answer is right here we can't find

1094.16

the variable navigation

1096

so while i copied and pasted the code i

1098.64

forgot to inject the navigation object

1101.84

from

1103.6

the navigation container

1106.48

now by adding it the navigation call

1108.72

here to open drawer works

1111.36

so now the menu call works

1118.48

all right any questions on this

1120.84

example so let's quickly review so we

1124.08

covered the different required

1125.76

dependencies for

1127.679

using react

1129.48

navigative navigation and that's a

1131.76

tongue twister

1133.919

in order for us to actually include

1135.76

things in our applications we need to

1139.12

make sure that we've installed the react

1140.799

navigation library for navigate we need

1144.24

to include the drawer library and if we

1148.24

want to combine different components we

1150.32

need to also include things like the

1152.559

stack button

1154.72

and any other components that we need

1156.32

for our applications

1162.48

as always we'd like to thank you for

1164

your time

1165.76

if you would like to discuss any of

1167.12

these topics further or if you would

1169.28

like to give us ideas for additional

1172.08

topics to talk about you can reach us at

1174.72

our email address at info developer.com

1177.52

you can find us on our website at

1179.6

developernerd.com contact us you can

1182.32

find us on youtube by googling

1184.48

developerner

1186.72

space youtube you can also find us on

1189.28

vimeo at vimeo.com developer nerd our

1192.88

goal is making every developer better

1195.12

thank you and have a great day

1214.32

you