Press "Enter" to skip to content

React JS Tutorial – #8 Parent-Child Communication


now back in here basically the idea is
that when you click on the pause button
you want to make the pause properly on
the state true because you basically
pause the timer on the same note if you
click on the resume button you want to
make the pass property of false because
the countdown timer will no longer
because it’s gonna be active okay so
let’s do the following here so the way
you communicate between the countdown
timer itself countdown Jia’s and
controls yes the controls component
right the way you do that is also
through properties so here we have a
paused property and that’s basically a
representation of the state inside of
the countdown timer
the thing about state is that state is
internal or private so the state that we
have in countdown timer is only private
to that specific component it’s not
visible to any components either above
countdown timer like app GS because
because countdown timer is a child of AB
GS but it’s also not available to
control she is because controls GS is a
child of countdown so the state inside
countdown it’s only available to
countdown GS component or file itself so
to make that state available we
basically pass it through the props to
controls but now we also want to be able
to pass some kind of behavior for the
buttons and they behavior that I’m
talking about is going to the following
I’m going to add a new method let’s call
it toggle cost all this is going to do
is it’s basically going to set this
state okay and it’s going to basically
do the following it’s gonna set pause to
not this state pause so it’s going to
set the pause property on the state to
the opposite of whatever the pause
property was before on the state so if
it was false before it’s going to set it
to true it’s going to negate it if it
was true it’s going to make it false
okay so that’s what toggle means is
basically when to toggle a property
now we want to pass that method as a
property to controls so we’re gonna have
let’s call it on pause toggle and let’s
pass and that method actually we need to
do this dot double pause because we need
to reference the component right
and once that property is there we need
to also add it in here and let’s do this
I’m going to add that probably on the on
click event so whenever you click the
button that method is going to fire in
fact let’s go back to condom time error
let’s do console all clicked so we know
that once we click either one of the
buttons it’s going to execute this
method okay and let’s actually prove
that it works okay we get an error so
the ears here says that you cannot read
property set state of undefined the way
to fix it is to bind this component to
this function and one of the traditional
ways to do this would be to go back to
the constructor and do this toggle
paused equals this toggle paused dot
find this it’s basically going to bind
this keyword or basically this countdown
component to the method so that the this
keyword is going to be available inside
of the function the reason that we
experienced this issue is because this
instead of this function is not
available so we need to bind the
component or the this keyword to this
method so that this becomes available
inside of it okay so back in here let’s
click on pause we see quick so when we
move console.log from there now one
thing I’ll do in controls just for
visibility let’s also put these
properties on separate lines so that’s
more readable like so I’m also going to
align them like that let’s do the same
for the other button
and forth quick so just so that it’s
more readable okay
right so just to make this clear as you
saw the way that we make controls
dynamic so the way we make those buttons
actually work is that we pass a property
called
unpause toggle and this property allows
those buttons to have access to a toggle
passed method and once that method is
called and it’s actually going to be
called an every click whenever either
the buttons just clicked it’s going to
run at method and it’s going to update
the state whatever the pause probably
was before it’s going to be updated to
the exact opposite of its prior value
right so next let’s do the following
basically the idea is that if the value
of pause becomes true right if the
countdown timer needs to be paused we
need to stop it in this case we need to
clear the interval but if the pause
property becomes false
we basically need to set a new interval
right so let’s do the following here so
let’s do if this state paused is true we
can leave it like that
that’s basically going to be a truly
value and we’re going to do the
following if you need to be paused then
we basically need to clear the interval
else we’re going to set a new interval
right there one table made in controls
is a cold
the first argument pause instead of
paused because the property that we
passed is called tossed and that’s just
old what we need to fix
let’s change from pause to paused okay
that’s the one thing we need to fix now
back in Togo pause instead of using this
form of set state we’re actually going
to use a little bit different for so
let’s go to react is that world let’s go
through the documentation let’s go dogs
and let’s go to state and life cycle so
if there’s cooled down you’re going to
find a section here the title is State
updates may be asynchronous so what this
is talking about is it in some key
says it might be wrong to call the set
state directly with this work so
basically you call the set state method
and you pass a new property and it’s
going to update this state in simple
cases it works just fine but in more
complex cases when the updates of set
state are asynchronous and you need to
do something else afterwards you are
better off using a bit of a different
form the form that I’m talking about is
this instead of passing an object to set
state we can actually pass a function
that basically returns an object that
will update this day there’s also
another example here it’s basically the
same idea but instead we pass a
traditional function and then we do a
return with the object and of course
inside of that function we could do some
kind of a processing or logic that needs
to happen before that state is returned
and this is exactly the case for us
let’s go back in here and instead of
using this form I’m gonna do the
following let’s call this set state and
we’re gonna have a function here an
error function actually
so the error function if you go back to
the dogs as you can see it accepts
previous state ok previous state and it
also accepts props let me say these are
going to be the new props their path to
the set state function and so instead of
this function we need to return an
object eventually that will basically
have an update to the pause property so
what needs to happen is that let’s first
create a variable we’re gonna call it
paused and this is basically going to be
the opposite of the pause property on
the previous stage okay in the end we
want to pass the post variable to this
object so that it’s going to update the
state and this form with a true curly
braces and then pause is the same thing
as doing paused callin pause this is
just a shorthand property that we
covered in es6 series on this channel
once we have the new value for the pause
property on the state
have a simple if check here so if we
need to pause the timer in this case we
will clear the interval the old interval
basically and this is going to stop the
timer so the interval is not going to
execute anymore so it won’t be updating
the state every second right otherwise
we need to actually set a new interval
so I’m going to copy that from component
dip mount and this will basically set
the interval every second it’s going to
set our state to the new value of
whatever the get remaining time returns
which is basically going to be a
difference between of two days right
once you say that let’s go back and I
think this should work let’s go back to
the app previous is not defined let’s
see it must have made it simple of
course it’s not previous its previous
state okay let’s go back let’s click on
the pause button as you can see right
here it pauses a timer actually if you
click on app let’s expand it let’s go to
countdown and you see that the pause
property becomes true now what if you
click on resume button that’s going to
toggle the property to false and the
countdown timer is going to resume it’s
a very simple you click pause it stops
update the property when you click
resume again it updates the property but
it updates the property from true to
false okay very simple and the key here
as I said is we need to do some
processing just before we return the
object and this is why we use the
alternative form of set state where we
pass a function that basically needs to
do something
along with updating the state okay now
the other thing I’ll do is I’ll stick to
a bit of a different pattern for calling
properties as well as methods and so in
this case typical needs for properties
are for example on click on key down
things of that nature these are perfect
for events so this event on pause toggle
it’s not an actual event on the Dom
element right so for example on click is
the actual event so we don’t have to
define on click here it’s a built-in
event in react it’s going to fire
whenever you click
in this case this event is going to fire
whenever you call the dad method from he
controls right on pals taco
instead of doing toggle clause I’m gonna
do handle paused toggle she has to
follow the same on something like on
click for example if we had on clicked
we have handle on clicked that’s just a
pattern to follow so use the on and
handle prefix on prefix for your events
handle for your methods so we’ll just
call it handle pause toggle and I’ll
rename this as follows okay and we also
need to update the constructor let’s
update the names and again this is just
one of the ways to do this in fact I’m
gonna come this out and I’m going to
show you a few other different ways that
you could have done this so the other
thing we could have done is instead of
using that binding in the constructor we
could also use a bit of a different
format for functions so instead of doing
it with their shorthand methods syntax
I’m going to use the es7 class property
syntax so in this case you would
basically be creating a property called
handle pause toggle and that property is
going to be referencing a function so
when you say that this form actually
allows you to have this keyword have the
proper context where the proper value
that refers to the current component
without actually needing to bind it to
anything as we did in the constructor so
you go back in here if you click on
pause this is still going to work click
resume and it’s going to resume the time
this is a bit of a different form and
I’m actually going to leave it off like
that this is the new es7 form and it’s
valid it actually works here because
we’re using Babel and Babylon the scenes is going to transform that

Please follow and like us: