Press "Enter" to skip to content

React JS Tutorial – #5 State Management


but so we got our first functional
component going here someone remove the
title here let’s do here is coming up
okay I’m gonna remove the second title
let’s see how that looks okay looks fine
to me
the other thing I’d like to do is to
primary that like I think I’m gonna do
is info and that I’m pretty sure that’s
gonna be blue and the other thing I’ll
do is a lot of class is bold so this is
basically going to add a subtle gradient
as you can see here right so if you look
for is bold you’re gonna see it here
basically adds a gradient so it looks
better okay the other thing I’m going to
do is so this is going to be a countdown
timer so we’re gonna have separate
sections for you know days hours minutes
and seconds so I’m gonna look for its go
to Docs and I look for layouts let’s go
to level so level is exactly what we
need it basically is just a bunch of
different sections or blocks here divide
it so I’m gonna go in there and just
copy that okay and I’ll probably just
have it see I think I’m gonna have
another div here the class name of
section perhaps
okay just let’s put that in let’s adjust
the spacing here of course we got to
replace the glasses here with the
colossi and I’m gonna select the whole
word so that it doesn’t replace class
name okay
so let’s do that and now it should work
yeah that’s that’s good to me the other
thing I’ll do is I’ll also add has text
centered and that’s gonna set it the
title it right there for us alright so
the next thing I’m going to do is let’s
have days here the next one with the
hours then after that we’ll have minutes
and less we’re gonna have seconds okay
it said before we began with a
functional component typically it’s a
good practice to begin with functional
components and as your application grows
you can always convert them to
class-based components if you feel like
you need two functional components are
more success and more efficient because
they render faster but if you compare
them to cloud-based components the
latter are more versatile because they
allow you to have lifecycle hooks and
not only that but also state so as you
can see right here we don’t have any
properties really and we don’t have any
state but for this application we want
to do is we want to calculate the
difference between the current time as
well as the date of the new year and
this is not quite possible with
functional components so I think what
I’m going to do next is I’m going to
convert these to a class-based component
because we need to store the interval
with the difference between now and the
future date in the state itself so what
I’m going to do is a minute of class
let’s convert this to a class countdown
extends react dot components okay
can be a class for us and that class
needs to have a render method needs to
implement that let’s grab the section
and let’s put the section inside the
render method okay remove the countdown
and in fact we can bring the default
exports to the same line like so let’s
save it it should still work just fine
nope it doesn’t do you render method
should have a translator oh yeah of
course the rather method needs to have
returned just run with that and this
should still work let’s go back yep that
works and the other thing when I do I’m
just gonna use the native port for
component like so cool component without
react so that’s gonna be let’s move this
to right just a little bit and this is
what it looks like so like I said we
need to have state now typically the
first thing you would do is you would
have your constructor so just like with
any es6 class you have a constructor and
dr structure because we extend the
built-in component class our countdown
subclass is going to have a constructor
that also accepts properties and it
needs to pass those properties to the
parent constructor like so so we need to
call the director structure with a super
keyword and pass the properties and then
we’re gonna do this state and our state
is basically going to be an object that
contains properties as well as the
values of those properties that refer to
the state values or essentially the
state data that we need to maintain in
this scenario we’re gonna have an
interval so like I said before this
integral is basically going to be a
difference between the current date as
well as a date in the future in this
case it’s going to be the date of the
new year so perhaps what we’re gonna do
is we want to make a call to a local
method let’s do this get romaine
I’m okay and I’m going to create that
method in here and the class like so and
let’s do a few things so this is where
it gets a little bit complicated we
could still use the built in date object
in fact let’s try doing that so we have
the variable up now that’s gonna be a
new date and then we could have new year
which would basically be new date and I
could do now dot get full here plus one
that’s going to be the first argument
and then we’re gonna have because we’re
dealing with month here the month
actually in date object begin let’s zero
so January would be at index zero and
then we have the first this debates for
the date of the month okay so these two
let’s do console log here new year what
I’m gonna do is I’m just gonna make this
goal for now let’s comment this out and
let’s do let’s just call that method
let’s see what it gives us okay so it
gives us two dates here one of them is
basically it’s your date right now
January 7th of 2018 at that time and the
next year is going to be January 1st
2019 it’s possible to calculate the
difference between the dates using plain
JavaScript but the built in date object
is not versatile enough to account for
things like daylight time saving or leap
years and it’s a little bit tedious and
more complicated if you do it manually
it’s gonna be much easier if we pull in
a library that’s gonna calculate a
little logic by itself so one of the
libraries were doing that is known as
moment yes so in fact let’s go to moment
here yes let’s open up the docs and you
as you can see here one with GS allows
you to work with the native data object
but it also provides you with a lot of
different methods for working with dates
as well as working with dates and
different formats calculating
differences between dates and
as I said in our example we hear about
is the intervals or the difference
between the dates as well as being able
to calculate that difference in days
minutes hours as well as seconds so this
is what we’re interested in here so it’s
to gonna stop the server so what I’m
gonna do is I’m gonna do NPM install
that safe moment okay so I’m gonna
remove all of that stuff just say no and
I’m gonna bring this back to the method
call we’re gonna refactor it in just a
moment let’s just make sure that the
library installs okay so I’m gonna do in
p.m. start let’s get the server up and
running again
let’s close everything else okay open up
the dev tools okay so what we can do it
here because we already have a moment
I’m gonna important moment from the NPM
modules I’m gonna do a moment from
moment so that’s gonna be a library and
next I’m gonna do the following let’s do
a few things here so the first thing
you’ll have a variable that represents
the timeout now so there’s going to be a
call to the moment constructor or
library the next thing is going to be a
variable to represent new year
so that’s going to be again call to
moment but in this case what I’m gonna
do is I’m gonna pass an object I mean I
specify that the year is going to be the
year of basically now plus one and then
lastly we’re gonna calculate a
difference and this is just going to be
the difference between new year right as
well as now basically let’s say that and
let’s do return moment duration of the
difference right so let’s go back to the
documentation let’s do a moment def
just to see what this is gonna give us
let’s search for difference let’s see
yeah right there on the right as you can
see so we can basically with the moment
we can basically calculate a difference
between two dates as you can see right
here for example the first date the
second one and then you when you call
the difference the diff method is going
to give you the number of milliseconds
that’s basically the length of the
interval between the two dates and the
next thing because we want to have
access to the number of days met hours
and seconds we need to convert that
result of milliseconds to an actual
interval to an actual duration so in
this case if you go to durations as you
can see again the constructor is pretty
versatile so you could pass different
objects but in this case we passed the
number of milliseconds and that’s gonna
work by default so and once we have that
we could actually access things like
days right so to call days method on the
duration that’s gonna give us the number
of days you call hours minutes as well
as seconds right so back in the editor
so we’ve got our duration here and the
duration is basically going to be
returned from the method and it’s going
to be assigned to the interval in fact
I’m going to call it duration just to be
clear and then here in the return method
let’s do the following let’s have our
duration and this is basically going to
be this state tuition okay in fact we
could also use the structuring and then
you’ll note the duration from there save
it now once we have that we’re going to
actually start making calls in here so
this is basically a set X NGS text it
allows you to output you know different
expressions inside of GSX inside your
markup so I’m going to do duration dot
days let’s see if that works actually
let’s go back to the application
we have 23 days which is weird you know
I think the reason that we get 23 is
because it also tries to calculate
things like month and weeks
in fact what we would want to do in this
case you would actually want to call as
days instead of days and once we have
that I think let’s see that gives us
that’s gonna give us another like so so
it’s to the following here I’m gonna
call math dot war on it okay
and this should basically bring it down
to the nearest integer so that’s gonna
get 358 and that makes a lot more sense
now for hours it’s the duration dot
hours okay let’s have minutes so this is
going to be duration dot minutes okay
and let’s it would have seconds so this
is going to be creation seconds and
let’s see how that works
that makes sense now could also be nice
to me could have preceding zeros here so
for example if you I would say five
seconds left right we want to make sure
that we add a zero just before it so the
easiest way to do this is to use the new
bad start method that was added in Atma
script 2017 so this is very recent but
it’s a useful method because it
basically allows you to prepend a shrink
to the beginning of another string so
because in this case once you call for
example hours that’s gonna give you a
number one thing we could do is we could
call to strength right and we could then
do bad start I want the length to be two
because the maximum number of hours is
going to be 23 and that’s basically a
two digit number so we’re going to have
that start length of two and then the
string is going to be zero and then
we’re gonna do the same thing for
minutes as well as seconds okay
let’s go back to the application yep and
I think it worried works because you can see it works for seconds

Please follow and like us: