I don't need mapStateToProps in my component but I need mapDispatchToProps -
const mapDispatchToProps = dispatch => ({ myCallBack(passFunc, passDirFunc) { dispatch( actions.setSideNavAction(passFunc, passDirFunc) ) } })
If I remove mapStateToProps completely and use connect like -
export default connect(mapDispatchToProps)(Application);
then getting error - Uncaught (in promise) TypeError: dispatch is not a function.
Is it mandatory to keep an empty mapStateToProps.
const mapStateToProps = state => ({ })
Please clarify .
Can I mapDispatchToProps without mapStateToProps in Redux? Yes. You can skip the first parameter by passing undefined or null .
The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.
Without mapDispatchToPropsNotice that the component receives a dispatch prop, which comes from connect() , and then has to use it directly to trigger the actions.
You can definitely use mapStateToProps with a functional component, the same way you would with a class component.
You can just pass a null
to the connect
:
export default connect(null, mapDispatchToProps)(Application);
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