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.
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;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With