React Redux form and connect syntax

試著忘記壹切 提交于 2021-01-21 08:58:07

问题


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)

回答1:


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)



回答2:


Applying multiple middlewares works like this:

middlewareB(middlewareA(Component))

In your case:

export default connect(select, actions)(reduxForm({})(ChangePassword))



回答3:


What you can do is create a wrapper class above the redux form and connect that to redux. And you can simply pass the props from that wrapper class to the redux form. Something like this:

//redux form
export class ChangePassword extends Component {
  ..omitted for clarity
}

export default reduxForm({
  form: 'change_password_form',
  validate
})(ChangePassword);

// wrapper class
export class FormWrapper extends React.Component {
  // omitted for clarity
}

export default connect(select, actions)(FormWrapper);



回答4:


I can guarantee this works, TypeScript herein optional:

(I am using combineReducer, hence the nested state imported to the component props.)

Here <any, any, any> is respectively, mapStateToProps, imported Action Creators, and the React Component wrapped with Redux Form.

const mapStateToProps = (state: any) => ({
  currentItems: state.itemsReducer.currentItem,
  initialValues: state.itemsReducer.currentItem,
  keepDirtyOnReinitialize: true,
});

//ReduxForm decorator populates the initialValues prop 
//from Connect into Field components with its library functionality:
export default connect<any, any, any>(mapStateToProps, {
  CreateItem,
  UpdateItem,
  getItemById
})(reduxForm({
  form: 'ItemsForm'
})(AddItemToList))


来源:https://stackoverflow.com/questions/46791190/react-redux-form-and-connect-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!