Press "Enter" to skip to content

React Router v4 Tutorial – #2 Routes & Links


so now that we have sort of like a
database of writers here let’s actually
go back to the app component what I’m
going to do is let’s create a component
and dead amount here inside of that
method I’d like to fetch all the writers
so that we can actually set them on the
state so what I’ll use here is the new
fetch API and I’m going to go to HTTP
localhost 2004 and then I will basically
go to writers so once that finishes I
will call the then go back we’ll get a
response and then what typically happens
here is when you get a response you’d
like to convert it to JSON so the first
thing you might do is you might return
response in JSON like that and then
finally as the second callback once you
do get that response it will contain the
list of writers and once you get those
writers you can finally call this set
state with the writers like that had a
few spaces there for clarity and the
only thing is we need to remove the s
because we are using HTTP and HTTPS so
let me say that and then if you go back
to the eternal the one thing we could do
here of course is we’ve got the server
running so we can just simply start out
our applications all do yarn start this
will start the application and will be
running in the browser so I’ll pull a
everything is fine let’s see if it made
the request wasn’t expected so we made a
get to writers and we got the list of
writers as we expected now would also be
nice if we could actually just do a yarn
start and this would start the prototype
application as well as the fake JSON
server that we install so the one thing
that we could do is if we go back to
package JSON over here we do have the
start command what we could actually do
is we can actually add another command
here and if you’re using a unix-like
system you could use the a percent
symbol and then include another command
so what we can do here is we can
actually call the JSON server itself
with the watch flag and this with points
to store in JSON and then finally the
port needs to be three thousand four
otherwise it’s basically
to run because the front end application
also runs on port 3000 so we need to
provide a custom board for that and then
also note that we don’t have to provide
the node modules thing with Leben folder
because that path is already being
resolved if that gets JSON for us so
let’s save that and let’s go back to the
terminal I’m going to stop everything
else here and let’s stop the server as
well clear the log and I’ll just simply
do your start so this should start the
server and this should also start with
the content application and it could
cause that let’s open up the dev tools
and let’s refresh and as you can see
here to make some requests to writers
and request exceeds so everything is
running fine now we’ve got the content
as well as feedback and writing at the
same time now that can have Jia’s here
this is one of the typical forms of
using the fetch API that you would see
but instead of doing that I’d like to
actually use the new async await us in
fact so what I’ll do is I’ll create a
constant writers and then we’ll make the
fetch request to the writers but then
we’ll basically await for it until it
finishes and then once that’s done so
I’m going to wrap this in parentheses
and once that’s done I’m actually going
to call it the JSON method on it and of
course we need to await as well because
all those operations are asynchronous
and in fact that’s actually do cost a
lot writers to see if that actually
works back in the app you can see here
we get an error and that’s because we
need to add another keyword but it’s
known as a sink to couple in it did
mount and that’s actually going to make
this function work with the async await
effects
they have it I believe in es feet let’s
go hard to keep up but let’s save the
file and let’s go back and as you can
see here everything works so finally
what I’ll do is I’m also going to set
the state so let’s do this set state and
we’ll just basically pass into these
writers on it like that so once we do
have the writers in our application
state what I’d like to do next is I’d
like to create an order list let’s have
a few list items here and then here is
basically where I would like to have the
links so it’s actually important browser
router here we get router down like that
and once we have that browser router
this is basically the component that it
needs to be used as the wrapping element
for the entire application so before we
actually have any GSX or any j6 markup
we need to wrap everything in browser
router in order to enable writing in our
application so that’s the first step we
need to do I’ll bring back we do as we
can’t before but now everything is
wrapped in this router and if you save
and if you go back basically nothing
changed it’s going to behave just like
before you’re not going to see any
changes but once we do that this is
deployment we can actually define the
links and of course you could default to
anchor tags the only problem with that
is that once you create those anchor
tags and let’s actually create if you
here also say writers ELISA be the link
to our writers and let’s just save
writers let’s say that the only problem
of that setup also click on that link as
you can see here the application is
going to refresh the issue with that is
that we’re actually creating a single
page application where sba was single
page applications there’s basically no
word precious
and what typically happens when you
click a link is the URL itself changes
but then parts of the application will
be rendered based on what Brock has
actually been matched by the application
so the old routing system is being
managed on the content instead of the
backend and that’s why you don’t see the
major crash as you would typically see
in multi-page applications because in
those applications what typically
happens is you ask for a page from the
server and the server returns back all
of the HTML CSS and JavaScript for that
page and then you have a full refresh
the page refreshes and then you see the
contents of this page but like I said
we’re building a single page application
here and that’s why there’s going to be
no they do freshers and for that exact
reason there’s actually a specialized
component that’s known as link and the
link will basically allow us to have the
anchor tag as you might expect and the
link needs a few parameters here so the
first one will be a prop the last two
this will basically be the path so let’s
just simply do slash we might call it
home so this will be the route to home
and then let’s also create another link
and this link will be pointing it to the
writers so I’ll do slash writers like
that indented a little bit and let’s go
back to the browser now we have two
links so one of them comes to home one
of them to writers now notice that once
they click on those links we don’t get
any picture fresh and that’s exactly
what I was talking about the alengka
component what it basically does is it
creates an anchor element if you inspect
that you’re actually gonna see it right
there or the a tripod but then when you
actually click on the link it’s going to
prevent the default behavior and then
also update the URL once we do have the
links would be nice to actually display
different content based on what the link
has been matched so first of all instead
of having a div here I’m going to switch
to a fragment because that’s not what I
like to use you don’t actually need to
need this and for the fragment of course
we’ll need to import it as well react
and then inside of that fragment what
I’d like to do is to have several
sections so one could be a section for
home and the other section would be free
writers so I’d like to see different
sections based on the different URL they
were coming on for example this could be
wrapped in a div right and inside of
that dad let’s just say we have our home
okay
that and then we have another div just
below it where we put our writers and
then we can delete all those other lines
that so these sections I want them to be
displayed conditionally based on the row
that we are currently on what react
router allows us to do at this point is
to import to a component that’s known as
route and then the rep component will
allow us to create definitions for the
routes in our application so we’ll just
have a simple route here and the path is
going to be the callback which is simply
slash and we’ll create another one
that’s going to match the writers so
I’ll just put slash writers and then to
instruct the route to display something
whenever that route is matched so they
slash route we need to do is we need to
provide one of the two properties
there’s actually three buddy – they’re
most commonly used our render as well as
component now with render it’s basically
a property that will expect a function
okay so we can provide a function like
that and for that function we can just
simply paste the mark-up that we have –
for instance a div let the text of home
will do the same thing for the second
route so I’ll do a render this will be a
function again and we’ll put a function
like that and let’s bring it back and D
you did inside of it so this will match
the writers and this is what we expect
to be displayed whenever me matched that
route basically let’s just add a space
for those lines
like that so we save it now let’s go
back to the browser when you land on the
homepage you’re gonna see a home being
displayed like that but when you switch
to writers you’re actually gonna see
writers now this is not exactly we
expect and the problem here is that
whenever you have several definitions of
rels so for instance this one matches
the slash path and this one matches the
writer’s path notice that both of them
start with a slash so when you are on
slash writers the first one is being
matched because it begins with the slash
griffix
but the second one is also be matched
because it exactly matches slash
rightist so the one thing we could do is
we could also add another attribute
that’s known as exact and this will
allow us to match the path exactly so if
I go back let’s see the difference right
now we are on writers if I switch to
home we’re gonna see home but then of
course when I click to the writers
length that’s gonna bring us to slash
writers and you’re gonna see the rarest section

Please follow and like us: