Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between stateful logic and state in React?

I'm going over the hooks documentation and they make it very clear

The state of these components is completely independent. Hooks are a way to reuse stateful logic, not state itself.

I want to be clear on the difference between these two things.

like image 436
VK1 Avatar asked Mar 13 '19 02:03

VK1


1 Answers

That's saying that when you reuse a custom hook in multiple components, the actual state data will not be shared between those components. There will be a separate instance of the state data for each use of the hook.

If you do want to share the state, then you would be looking at using the useContext hook (which could also be used in a custom hook which would then mean the multiple uses of the hook could share state).

like image 146
lecstor Avatar answered Sep 28 '22 02:09

lecstor