I have a React component
export class Login extends Component {
..omitted for clarity
}
export default connect(select, actions)(Login);
And as can be seen it connects to Redux and it works perfectly well
I have Redux Form as
export class ChangePassword extends Component {
..omitted for clarity
}
export default reduxForm({
form: 'change_password_form',
validate
})(ChangePassword);
Again this is working perfectly well.
My question is , I can't work out the syntax to have the Redux form also to use the connect statement e.g. connect(select, actions)
Something like this ?
export default reduxForm({
form: 'change_password_form',
validate
}).connect(select, actions)(ChangePassword)
The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.
The React Redux connect() API is used for creating container elements that are connected to the Redux store. The Redux store is derived from the topmost ancestor of the component using React Context. If you are only creating a presentational component, you have no need for connect() .
mapStateToProps() is a function used to provide the store data to your component. On the other hand, mapDispatchToProps() is used to provide the action creators as props to your component.
Since the connect
method decorates the original component, and returns a decorated component, you can pass that component to reduxForm
(Redux Form FAQ).
You can see an example in the docs.
const decoratedComponent = connect(select, actions)(ChangePassword)
export default reduxForm({
form: 'change_password_form',
validate
})(DecoratedComponent)
Or pass it directly:
export default reduxForm({
form: 'change_password_form',
validate
})(connect(select, actions)(ChangePassword))
If you need to get data from the store, for example - to create the form's initial values from the state, you can wrap the original component with redux-form, and pass the "form" component to connect
:
export default connect(select, actions)(reduxForm({
form: 'change_password_form',
validate
})(ChangePassword))
Or you can use functional composing with a compose
method (from redux/lodash/ramda for example), to call reduxForm
, and then connect
on the component:
export default compose(
connect(mapStateToProps, mapDispatchToProps),
reduxForm({ form: 'change_password_form', validate })
)(ChangePassword)
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