TypeScript, redux-form and connect

点点圈 提交于 2019-12-06 04:03:12

问题


First of all, I'm very new to TS, React & Redux, so sorry if this is an obvious question.

I'm trying to modify this example to get a form to load some information. It's using redux-form.

I'm trying to figure out how to call connect and redux-form at the same time in the export of the component. Right now it's looking like this:

class UserForm extends React.Component<IUserProps, void> { .. }

export default ReduxForm.reduxForm({
  form: 'user',
  fields: [
    'userName',
    'password',
    'firstName',
    'lastName',
    'email'
  ],
  validate: UserForm.validate,
})(UserForm);

The examples I've seen without TS look like this:

class MyForm extends Component {}
MyForm = reduxForm(config)(MyForm)
MyForm = connect(mapStateToProps, mapDispatchToProps)(MyForm)
export default MyForm

But if I try to do the same in TS I get the TS2300 error: duplicate identifier.

I've also tried to use the @connect decorator, but I couldn't make it work (or find any working example online).


回答1:


Can you just chain them?

class UserForm extends React.Component<IUserProps, void> { .. }

export default connect(mapStateToProps,mapDispatchToProps) 
    (ReduxForm.reduxForm({
  form: 'user',
  fields: [
    'userName',
    'password',
    'firstName',
    'lastName',
    'email'
  ],
  validate: UserForm.validate,
})(UserForm));



回答2:


not a fix, but issue can be silenced by casting to any

connect(mapStateToProps, mapDispatchToProps)(MyForm as any)



来源:https://stackoverflow.com/questions/40722592/typescript-redux-form-and-connect

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