Leo Lozes
Leo Lozes

Reputation: 1388

TypeScript, redux-form and connect

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

Upvotes: 5

Views: 3824

Answers (2)

zowers
zowers

Reputation: 587

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

connect(mapStateToProps, mapDispatchToProps)(MyForm as any)

Upvotes: 2

Alejandro C.
Alejandro C.

Reputation: 3801

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));

Upvotes: 5

Related Questions