DEV Community

Using custom hooks in place of "render props"

Nwakwoke Patrick Nnaemeka on April 24, 2019

One of the sweet but sometimes difficult to figure out part of React is reusing stateful logic across various components. Instead of rewriting ...
Collapse
 
rokkoo profile image
Alfonso

Nice post! Can you make another example using hooks to get user information (for example) across the other hooks?

Collapse
 
emeka profile image
Nwakwoke Patrick Nnaemeka

If I understand correctly you mean a hook that fetches user's information so that it is available for use across various components?

Collapse
 
rokkoo profile image
Alfonso

Yes, correct, I think it can be useful to understand more hooks with your examples.

Thread Thread
 
emeka profile image
Nwakwoke Patrick Nnaemeka

Alright, I will do something and send you a sandbox link, in the meantime, you should check my other article on useContext. That will also be helpful

Thread Thread
 
rokkoo profile image
Alfonso

I read your post about useContext and I learned a lot of, great explanation mate!

Thread Thread
 
seanmclem profile image
Seanmclem

What was that sandbox link? I'd love to see a custom hook Sharing a consistent State across components. I feel like you can only do it with context, no?

Thread Thread
 
emeka profile image
Nwakwoke Patrick Nnaemeka • Edited

If by consistent you mean a global state, then you can only do that with context or any store that holds global state. Just create a hook that consumes your store

Collapse
 
elie222 profile image
Elie

Awesome post. Well explained with a simple example.

Collapse
 
toddcoulson profile image
Todd Coulson

well done, well implemented

Collapse
 
evatkautz profile image
Eva Tkautz

Nice one!

Collapse
 
mikevb3 profile image
Miguel Villarreal

Thanks for sharing! it was really useful to grasp hooks