Reputation: 2516
I need to get Redux store data but not always and different data for each component instantiate. How can I use useSelector with Conditional statement?
The component should get data from store only when some child components are rendered and also different data each time depending on the child component.
Upvotes: 9
Views: 13097
Reputation: 1902
useSelector takes in a callback that has access to the redux state as an argument
so assuming you control whether the child component is rendered in a boolean flag called isComponentRendered, and want to select data when it is true and else nothing, you could try the following:
const data = useSelector(state => {
if(state.isComponentRendered) {
return state.data
}
return null;
})
Upvotes: 15