📺 Develpreneur YouTube Episode

Video + transcript

React Native Fundamentals - Part 13 - Draw Navigation Part 2

2021-11-04 •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]
okay so we have our app running here so
we will drop that guy there
we minimize visual code here
optimized
let's put it side by side so we can see
what's going on
all right now
since we created a blank template all of
our code currently exists for our app
lives in the apps
app.js
well that's open i will go ahead and
hide
our navigator because we won't need it
for the rest of this class
there we go
all right so for this first example
we need
to include
at least
a view
and we need to include at least uh what
else do i need i need
view and text
all right so view text for right now we
will
leave stylesheet off
and i will just take out the stylesheet
for now
we will put it back later but for now i
just want to simplify things
yeah so there we go
can everyone see the code is this big
enough
oh definitely
okay cool
i noticed in the recordings that
sometimes it was too small so just want
to make sure it's big enough
all right so we have our view we have
our text and we're pulling all that from
react native we have our function app
here which is the default so this is
actually what is being loaded on the
screen so we're loading this view with
this text and the status bar
so for our first example we want to
import
our
uh we need to
add a navigation container so
look for navigation
container
and if you're using visual code with all
the react native plugins installed from
the previous presentations uh it does
code complete for us so that makes it
very nice so here we did import
navigation container
from react native navigate
and this is if you try to import this
and you have not installed this library
your code will fail when you try to run
this
and it basically will say hey you're
missing the navigate library
and next we want to import
our drawer
so we want to create
a drawer applicator
and it also imports it from the react
navigation drawer library
all right
so the first thing i want to do is i
want to create three screens we need a
home screen a navigation screen and a
settings screen
so to create screens we'll just create
some functions
and for now we'll just call it stream
and inside of our screen we're going to
return
return
and we're going to return an object
which will be a view
and that view will contain some text
and i'm going to just
copy in some style for right now
so i'm going to copy in flex 1
which is going to say hey
just center it or put it in the center
of the app
of the container
align center and justify center so all
of our text will show up in the middle
all of our components
let's see
closing bracket there we go
that is what's called an inline style
we've talked about these previously
and then we'll have our text for our
screen
looks like we need three screens i'm
going to simplify this for now and we
will just duplicate this
and then it's going to yell at us
because we can't have the same function
names so we'll create a home screen
navigations
screen
notifications
screen
and a settings screen
so to make
tell us what screen we're on we will add
some text for each screen home
notifications
settings
now if we just save this
nothing changed that's because we have
not wired our screens up yet to the
application
just you know my styles
oh
i bet i dropped the style sheet there we
go
now it's happy okay
so i got rid of our styles
so our app
the default text is now at the top of
the screen
all right
so we have our screens
now we need to add our drawer
like in our presentation here
we need to create a navigation container
with our drawer navigation and then we
need to add our screens to it
so we'll come down here
right above our app
and we'll create a new constant for our
drawer so we don't have to constantly
call that create drawer navigator every
time
if you think in terms of java we just
basically created a new
variable uh and we are pointing to the
constructor
so now all we have to do is reference
drawer instead of having to type create
drawer navigator all over our code so
this is really cool how you can do this
in react native
all right so we take drawer
and we come down here to our return and
let's get rid of this view
so just like in
the presentation we need a navigation
container
and inside our navigation container we
want to add our drawer navigator
we have our drawer
get dot and our options our navigator or
screen
so we'll start by adding the navigator
controller
and we also want to give it a property
so we want to give it an initial route
to tell it what page to load first
and in this case we will say
home
now right now home is not defined
but we will define it in just a second
so this right here will tie to the
specific screen alias that we are going
to create
we need drawer again
and this time instead of navigator we
want a screen
and this can be a self-closing tag
and we need three of these
but we need a name
so we need to give it an alias so that
we can reference this screen throughout
our application
we'll call it home
called this one notifications
and this one settings
and then lastly we need to actually
tie this screen
to our actual screen component
so to do that we use the property
component
and we set it equal to
oops not quotes
use early brackets
and then you can just start typing and
it will find your function
new component again
use our object block
and
start typing so there's notification or
not uh
[Music]
yeah that's what i want notification
screen
oh yeah not navigation
notification screen there we
go i gotta be careful when you use the
auto suggestions because sometimes you
might not be on the one you want
now what will help you identify the one
you want is the purple represents
functions that you have defined
so that tells me that this is the
settings screen that i want
all right now if i save this we should
see
our home page
and nothing else
now if i swipe to the right there is my
drawer so now i have home
notification
and settings so i can navigate between
the three screens using my drawer
and that is an example of how to set up
using
a drawer
okay does it anyone have any questions
on the first example
okay
if you did you are a mute if not i'll
move on to the second example
so our second example we're going to mix
things up a little bit so we're going to
actually mix some navigations with our
drawer so we're going to add those
buttons to our page that allows us to
click from home
to notifications from notifications to
home and from settings to home
all right so now we need to add
button to our imports
all right so now
we need to do two things
one
we need to
uh so because we're using the navigation
container
by default
it's going to pass the navigation
object
to each of these components
however
because right now each of these
components do not have any imports into
their functions we cannot use that
navigation property or that navigation
object
so to get the navigator or navigation we
just add the object
navigation
and this will now allow us to use that
navigation
property
and to use that navigation property
we're going to add a button
and our button is going to have an on
press event
and we will create an object
we will create a call
to an inline
all or an inline javascript
all essentially
and here we're going to call navigation
dot
navigate
and now
because we are passing the navigation
from our
container here
we have in the navigation the ability to
navigate to these aliases so we have
home notifications and settings
so here we will navigate to
notifications
and then we also want to add
another property for title
and we want to give it some text to our
button so go to
notifications
and then we need to close our button
added an on press
it has the navigate
our navigate object using the navigate
function to navigate to notifications
so if we just save this we see we now
have a button here go to notifications
and if i click this it will take me to
the notifications page however we have
not added the button to go back yet so
we're kind of stuck here
so let's copy this button
go to our notifications
go to our settings
those settings let's go to home
navigate back to home
and notifications we also want to go
back to home
okay notifications home home
settings
if i save this
we now see we have the button on
notifications and if we click the button
we now go back to the home page
now if we navigate using our drawer and
go to settings we can also quickly jump
back to the home but we can't get to
settings so we that's where this menu or
drawer comes in handy
we can still jump
your individual pages through the menu
through the drawer
or we can use the individual buttons for
navigation that we've added
to our application
any questions on how we mix the button
navigations
with
the door navigations using the
navigation object
you
Transcript Segments
0.46

[Music]

26.64

okay so we have our app running here so

30.48

we will drop that guy there

33.52

we minimize visual code here

36.88

optimized

39.6

let's put it side by side so we can see

41.44

what's going on

43.68

all right now

45.68

since we created a blank template all of

48.239

our code currently exists for our app

51.12

lives in the apps

53.52

app.js

55.68

well that's open i will go ahead and

58.079

hide

59.12

our navigator because we won't need it

60.8

for the rest of this class

68.159

there we go

72.4

all right so for this first example

74.88

we need

75.92

to include

77.6

at least

78.799

a view

81.119

and we need to include at least uh what

84.24

else do i need i need

87.119

view and text

98

all right so view text for right now we

100.88

will

101.68

leave stylesheet off

106.479

and i will just take out the stylesheet

108.399

for now

111.52

we will put it back later but for now i

113.439

just want to simplify things

117.759

yeah so there we go

123.84

can everyone see the code is this big

125.92

enough

130.479

oh definitely

131.84

okay cool

133.44

i noticed in the recordings that

135.52

sometimes it was too small so just want

137.52

to make sure it's big enough

140

all right so we have our view we have

142.879

our text and we're pulling all that from

145.04

react native we have our function app

147.68

here which is the default so this is

150.16

actually what is being loaded on the

152.08

screen so we're loading this view with

154.08

this text and the status bar

157.76

so for our first example we want to

160.239

import

162.08

our

164.56

uh we need to

165.92

add a navigation container so

168.64

look for navigation

171.28

container

173.44

and if you're using visual code with all

175.599

the react native plugins installed from

177.68

the previous presentations uh it does

180.879

code complete for us so that makes it

182.8

very nice so here we did import

185.76

navigation container

188.08

from react native navigate

190.8

and this is if you try to import this

194.48

and you have not installed this library

197.28

your code will fail when you try to run

199.04

this

200.239

and it basically will say hey you're

201.76

missing the navigate library

204.799

and next we want to import

207.36

our drawer

208.799

so we want to create

212.48

a drawer applicator

215.04

and it also imports it from the react

217.28

navigation drawer library

222.799

all right

224.159

so the first thing i want to do is i

226.159

want to create three screens we need a

228.48

home screen a navigation screen and a

231.2

settings screen

232.56

so to create screens we'll just create

234.159

some functions

236.799

and for now we'll just call it stream

245.92

and inside of our screen we're going to

247.92

return

252.4

return

255.04

and we're going to return an object

259.199

which will be a view

265.199

and that view will contain some text

273.759

and i'm going to just

277.84

copy in some style for right now

283.04

so i'm going to copy in flex 1

285.919

which is going to say hey

288.479

just center it or put it in the center

290.88

of the app

291.919

of the container

294

align center and justify center so all

296.96

of our text will show up in the middle

298.639

all of our components

303.28

let's see

304.24

closing bracket there we go

307.44

that is what's called an inline style

309.759

we've talked about these previously

312.24

and then we'll have our text for our

314.32

screen

316

looks like we need three screens i'm

317.52

going to simplify this for now and we

319.6

will just duplicate this

322.16

and then it's going to yell at us

323.52

because we can't have the same function

325.36

names so we'll create a home screen

331.52

navigations

334.16

screen

338.88

notifications

342

screen

346.479

and a settings screen

352.24

so to make

353.6

tell us what screen we're on we will add

355.44

some text for each screen home

360.72

notifications

366.08

settings

367.44

now if we just save this

369.12

nothing changed that's because we have

371.039

not wired our screens up yet to the

373.44

application

377.84

just you know my styles

384.16

oh

385.759

i bet i dropped the style sheet there we

389.52

go

390.96

now it's happy okay

392.639

so i got rid of our styles

394.96

so our app

396.56

the default text is now at the top of

398.56

the screen

400.8

all right

401.68

so we have our screens

404.88

now we need to add our drawer

408.56

like in our presentation here

413.68

we need to create a navigation container

416.56

with our drawer navigation and then we

419.199

need to add our screens to it

423.28

so we'll come down here

425.199

right above our app

428.479

and we'll create a new constant for our

430.639

drawer so we don't have to constantly

432.56

call that create drawer navigator every

434.72

time

444.4

if you think in terms of java we just

446.16

basically created a new

448.84

variable uh and we are pointing to the

451.68

constructor

452.96

so now all we have to do is reference

454.639

drawer instead of having to type create

456.479

drawer navigator all over our code so

459.28

this is really cool how you can do this

461.039

in react native

463.599

all right so we take drawer

466.4

and we come down here to our return and

468.879

let's get rid of this view

471.599

so just like in

473.28

the presentation we need a navigation

477.039

container

482.72

and inside our navigation container we

484.879

want to add our drawer navigator

488.4

we have our drawer

490.56

get dot and our options our navigator or

493.759

screen

494.72

so we'll start by adding the navigator

496.72

controller

499.039

and we also want to give it a property

502

so we want to give it an initial route

504.08

to tell it what page to load first

507.12

and in this case we will say

509.12

home

510.56

now right now home is not defined

514.32

but we will define it in just a second

517.36

so this right here will tie to the

520.24

specific screen alias that we are going

522.56

to create

525.44

we need drawer again

527.6

and this time instead of navigator we

529.6

want a screen

535.36

and this can be a self-closing tag

538.32

and we need three of these

541.6

but we need a name

544.24

so we need to give it an alias so that

546.16

we can reference this screen throughout

548.48

our application

549.839

we'll call it home

552.8

called this one notifications

560.399

and this one settings

567.839

and then lastly we need to actually

570.399

tie this screen

572.399

to our actual screen component

576.959

so to do that we use the property

578.959

component

582

and we set it equal to

588.72

oops not quotes

591.6

use early brackets

593.76

and then you can just start typing and

595.839

it will find your function

601.36

new component again

605.2

use our object block

607.76

and

608.64

start typing so there's notification or

611.36

not uh

614.32

[Music]

619.92

yeah that's what i want notification

621.519

screen

625.839

oh yeah not navigation

628.32

notification screen there we

630.839

go i gotta be careful when you use the

635.04

auto suggestions because sometimes you

638.32

might not be on the one you want

640.959

now what will help you identify the one

643.04

you want is the purple represents

645.68

functions that you have defined

647.6

so that tells me that this is the

649.36

settings screen that i want

652.399

all right now if i save this we should

654.56

see

655.519

our home page

657.76

and nothing else

659.6

now if i swipe to the right there is my

662.8

drawer so now i have home

664.959

notification

667.839

and settings so i can navigate between

670.399

the three screens using my drawer

674.399

and that is an example of how to set up

676.72

using

677.68

a drawer

679.04

okay does it anyone have any questions

681.36

on the first example

683.92

okay

685.44

if you did you are a mute if not i'll

687.839

move on to the second example

690.72

so our second example we're going to mix

693.6

things up a little bit so we're going to

695.279

actually mix some navigations with our

697.92

drawer so we're going to add those

699.44

buttons to our page that allows us to

702.079

click from home

703.44

to notifications from notifications to

706.16

home and from settings to home

712.56

all right so now we need to add

714.839

button to our imports

729.36

all right so now

731.6

we need to do two things

733.68

one

734.56

we need to

735.92

uh so because we're using the navigation

738.8

container

740.079

by default

741.44

it's going to pass the navigation

744.72

object

746.24

to each of these components

748.32

however

749.44

because right now each of these

751.04

components do not have any imports into

753.68

their functions we cannot use that

755.839

navigation property or that navigation

758.399

object

759.36

so to get the navigator or navigation we

762.56

just add the object

767.36

navigation

771.279

and this will now allow us to use that

773.839

navigation

775.44

property

781.12

and to use that navigation property

782.88

we're going to add a button

786.16

and our button is going to have an on

788.32

press event

793.76

and we will create an object

797.04

we will create a call

800.16

to an inline

803.2

all or an inline javascript

805.76

all essentially

807.279

and here we're going to call navigation

810.48

dot

813.2

navigate

816.56

and now

817.68

because we are passing the navigation

821.519

from our

822.959

container here

824.48

we have in the navigation the ability to

827.839

navigate to these aliases so we have

829.92

home notifications and settings

832.88

so here we will navigate to

840

notifications

849.92

and then we also want to add

852.8

another property for title

857.44

and we want to give it some text to our

859.04

button so go to

861.6

notifications

864.8

and then we need to close our button

870.16

added an on press

873.6

it has the navigate

876.639

our navigate object using the navigate

878.959

function to navigate to notifications

882.079

so if we just save this we see we now

885.12

have a button here go to notifications

888

and if i click this it will take me to

889.92

the notifications page however we have

892.32

not added the button to go back yet so

894.399

we're kind of stuck here

896.079

so let's copy this button

901.44

go to our notifications

904.639

go to our settings

907.519

those settings let's go to home

912.88

navigate back to home

917.6

and notifications we also want to go

920

back to home

926.24

okay notifications home home

929.92

settings

931.759

if i save this

933.12

we now see we have the button on

934.72

notifications and if we click the button

937.44

we now go back to the home page

939.44

now if we navigate using our drawer and

941.92

go to settings we can also quickly jump

944.24

back to the home but we can't get to

946.32

settings so we that's where this menu or

950.16

drawer comes in handy

951.92

we can still jump

953.519

your individual pages through the menu

955.6

through the drawer

956.959

or we can use the individual buttons for

959.68

navigation that we've added

961.68

to our application

965.519

any questions on how we mix the button

968.48

navigations

969.839

with

971.199

the door navigations using the

973.199

navigation object

991.36

you