hello everyone so today i decided to
make a quick video on
how to pass props from your child
component to your parent component
and react but before something you're
like no you can't actually do that you
can't quite actually do
that yes yes i know that's why i called
it a hack
in the title is it a hack though i don't
know who cares right you've come here
because you basically want to find out
when something happens in your child
component or when you make something
happen in your child component
you want that to affect the parent
component right
okay so i'm going to show you how so
first off before we start it is my
developer duty
to tell you that you can't actually pass
props
from a child component to a parent
component you
can however pass functions so what we're
going to be doing is making the child
call a function that in turn changes the
state in the
parent component and once that state is
changed
you've guessed it the parent component
is going to pass that information down
as props
into the child got it good
in this super simple tutorial i'm going
to show you how to do this with react
hooks
so i'm going to be updating the word
parent and the parent component right
here
by clicking the button in the child
component
cool okay what are we waiting for let's
do this
okay so this is my setup for react as
you can
see i've already gone ahead and made a
parent and a child
the child hasn't been put inside the
parent yet
i'm going to be doing that in a little
bit i've imported
react for us to use i've also imported
some styling to make the parent look
like the green
box that it is and of course just put a
standard h1
parent uh h1 tag with the word parent in
it okay
now the child is similar it's got an h1
tag with the child
word in it so we know it's the child
that's simply just for us
and a button okay the button at the
moment has no functionality
but but it's that okay first things
first
let's go ahead and import the child for
us
to use so import
child from child there we go
that is done so now we've got the
child's component in here
let's go ahead and put it in our parent
component
so i'm just going to go ahead and put
the child component
right here there we go i actually don't
need that let's just close it off
okay so now if i click refresh
ah oops i've got the wrong uh path so
it's actually my components folder i
believe
so let's go ahead and find that so in
here components because that's where i
stuck it
standard mistake save and let's compile
that
okay so now we can visually see our
parent
with its child component inside and as i
said the button is there too
with no functionality now say i want to
do something
in the parent that will affect the child
because as we know props are passed down
from parent to child
if we want to reverse that well we can
actually use a function
so let's go ahead and do that now
in the child component we want to
add a function on click of the button
so on click and then let's
open this up like that on click we want
something to happen
now we know that on click we want to
change the parent word okay so let's go
ahead let's
put a function in here so this is an
arrow function
okay now we want to change words so this
is the function that i haven't written
yet
we want to change the word on the click
and we want to change the words to i
don't know
anya why not sure okay so now we have
this function here
obviously we are failing to compile
that's
natural what we need to actually do is
go
back to the parent here
and in the child component the function
that we wrote set that as a prop
so change word
let's open this up and now
we're gonna get that word or prop and we
are gonna
use set word
word so if you haven't uh used hooks
before i very much recommend
that you look into that now because i'm
going to be using the use states
so let's import that first use state
okay and i'm going to be updating the
state of this parent with the use
state hook so const
then word set
word so this is essentially what we've
got here if you
know hooks you will know that this set
word
will essentially in a way i guess set
this word to be whatever is passed
through here
okay use state and we're going to start
off with the state being
parent because that was our word parent
remember
so if i actually set that to here so i
when we start this up and we use state i
want the state of word
so i want the word to be parent so now
if i actually replace this with word
let's click save that will show up in
the browser
which we obviously can't see yet we
still have one more thing to do
and that is go back to the child and
pass props
because this is essentially if you
remember to the beginning of my video
we're going to be
invoking a function or calling a
function the child which will then in
turn
update the state here
so like that and then here and then we
need to pass it back down again
okay so that's what's happening we need
props
this function is also a prop so props
change words
let's click save okay
so now when i click the button here
let's actually maybe
make this more readable for you guys
okay so button click change
button
okay so now
when we click to change title ta-da
it is changing to anya now what i said
before
this word here is essentially whatever
we so if we refresh again so this is
back to the start
i can use the state to update this word
at the beginning
so i'm going to change this to bob and
if i click
save bob is there okay so bob
is our word use state bob
bob bob is our word now we want to
change
bob so we do this with the help of what
we've written here
here and here so now on click we invoke
a function
because this is an arrow function and we
use the function
to change the word in the state of
parent to anya
so there we go simple
okay so this was like a really quick
video it's just something that i found
quite useful
and i thought i'd address if any of you
were ever thinking about how to pass
props from child to
parent but hack because as we know
it's not really possible you use a
function
okay thanks so much for coding with me
if you enjoyed this video and find it
useful please do let me know so i know
to create more like this
uh do like and subscribe and i'll see
you soon
No comments:
Post a Comment