Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Redux form and connect syntax

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)
like image 638
Rory Avatar asked Oct 17 '17 13:10

Rory


People also ask

How can we use Connect From react redux?

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.

What is the purpose of the connect () function in Redux?

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() .

What is the difference between mapStateToProps () and mapDispatchToProps ()?

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.


1 Answers

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)
like image 89
Ori Drori Avatar answered Sep 22 '22 22:09

Ori Drori