Reputation: 857
My question is rather an opinion-based than fact-based.
I'm wondering what can be a better practice, My scenario is as follows:
I have a react component (which is a parent component) who uses some useSelector
to fetch store data and it has many children.
What is the best approach to consume data from the component children
?
React.useContext()
and pass it to the <*Context*.Provider/>
, and consume it as <*Context*.Consumer/>
useSelector()
inside the children to fetch the data from the store.Both of them will keep the data memoized, so I'm not worried about that... Both of them seem pretty good approach, I'm wondering what are the edge-cases for each of them?
Upvotes: 0
Views: 2686
Reputation: 26877
You use both (because useSelector
uses the Context API under the hood). That's why you have to have a parent <Provider store={store}>
element in order to use useSelector
.
So I think the easiest way would be to do useSelector
and let React Redux worry about the Context API.
Upvotes: 3
Reputation: 108
I struggled with the same question less that a week ago, according to me:
use the first one when the component that is using the data need to be updated when the data changes, using a consumer will re render the component when the consumed data changes anywhere in the application
Use the second one when you're just updating the data or just don't care if the data changes at a later time
Upvotes: 0