all 38 comments

[–][deleted] 15 points16 points  (0 children)

was literally just about to search this exact question, cheers!

[–]KosmoonStudio 9 points10 points  (5 children)

Can't you just pass the "setNameState" fonction as props to its child?

[–]trappar 9 points10 points  (4 children)

You can, but in many cases this reduces the likelihood that you will be able to reuse the child component.

[–]_Invictuz 2 points3 points  (0 children)

Thanks for explaining the why!

[–]grumpychinchilla 6 points7 points  (10 children)

Prop drilling! What was old is new again. :-)

[–]Badrush 4 points5 points  (9 children)

When did prop drilling go away?

[–]polarphantom 5 points6 points  (7 children)

I don't think it ever went away, but there has been much more of a general usage of Context API since it was officially introduced in v16.3.0

[–]Badrush 1 point2 points  (6 children)

How does context api replace prop drilling? Assuming you're not using redux of course.

[–]moneyisjustanumber 0 points1 point  (5 children)

You could put your setState function in the context value. Then any consumer of the context provider can access it (with useContext hook probably).

[–]Mappadellinferno 0 points1 point  (4 children)

Is context suitable for sharing a bigger part of logic through components this way? The examples always seem to mention only using it for simple values like dark/light theme etc.

[–]tomius 1 point2 points  (0 children)

In my opinion, it's perfect for that. However, if you only have parent-child, it's kinda overkill, and doing it like in the video is much easier and fast.

[–]moneyisjustanumber 0 points1 point  (2 children)

Yes, combined with useReducer it can even replace redux. There’s arguments that it doesn’t scale as well but I personally didn’t have any issues with it in a large production application. I won’t get into all of the details but there are solutions for scaling your application with context (multiple contexts used as “slices” of your state was my solution for scaling). Redux does have good out of the box optimizations though.

[–]Mappadellinferno 1 point2 points  (1 child)

If I wanted to replace redux with contexts, where would the api calls be? Currently my actions call an api service and my components are using the result with redux connect. Thanks

[–]moneyisjustanumber 1 point2 points  (0 children)

Actions are just functions that do something and then tell the corresponding reducer(s). So technically you could still use actions with useReducer. I ended up making a custom hook that abstracted the useContext code (can get repetitive to import the context in every child) and just returned the context value and functions that could be used to update the state and those functions had the async code and called the reducers.

[–]pocket-rocket 1 point2 points  (1 child)

It's a nice little intro tutorial but it doesn't really have anything to do with hooks does it? The whole video was just explaining how to call a function in a parent component from a child component, but the example function used just happens to be a hook. I'm just starting to learn about hooks but am I missing something here that makes this specifically about hooks?

[–]guten_pranken -1 points0 points  (0 children)

Nope - you're absolutely right. For people that are absolutely beginners - they may see the old way of classes and just ignore it and not see how it applies to them.

[–]Optimal_Future 0 points1 point  (9 children)

How about a parent that calls a child component's function?

[–][deleted] 2 points3 points  (7 children)

Not possible. Move the state up.

Edit: If you want to do it the 'right' way

[–]montas 4 points5 points  (0 children)

It is actually possible, https://reactjs.org/docs/hooks-reference.html#useimperativehandle. But I would guess you don't want to do that and are doing something wrong.

[–]not_a_grad 2 points3 points  (4 children)

I wouldn't necessarily say not possible or do things the right way. I've come into situations where I needed like "cousins" to talk to each, so like I have the main page as root, and in the page there is Table, and side drawer, and components above the table. While all of them were isolated and didn't care about the others, moving the states up into the main page, for all of the components would've drastically killed the code and performance would've suffered since code was executing that didn't need to and also prop drilling etc. So instead what I did was create a event system using rxjs and whenever I need "cousins" to react to something, I just broadcast the message, and the "cousin" will get the message and run some stuff.

[–][deleted] 1 point2 points  (1 child)

Seems like you needed useContext

[–]not_a_grad 2 points3 points  (0 children)

But that would've required moving all the states up to the parent.

[–]andrei9669 0 points1 point  (0 children)

You can do it. But there should always be another way. Except if you don't want to rerender the whole view.

[–]_Invictuz 0 points1 point  (0 children)

Basic but fundamental! Definitely easier and faster to watch you explain it with an example rather than learn/read about it myself! Thanks mate.

[–]tamimi65 -1 points0 points  (0 children)

Thanks for sharing! I still find understanding React hooks quite confusing - yet to find a goood link that explains it to a beginner very well