简体   繁体   中英

Redux form props & typescript

Was looking for years, didn't find anything worthy tho.

When I was working with flow, I could simply:

import { type FieldProps, FormProps } from 'redux-form';

Is there a similar (and that easy) way to properly set props to redux form in typescript?

Docs aren't saying anything about typescript, there's only a page for Flow typings.

However, I found that I can import something like propTypes from redux-form:

import { reduxForm, propTypes } from 'redux-form'

However - redux-form has nothing like propTypes exported, so docs are kinda deprecated.

Link: https://redux-form.com/7.2.1/docs/api/props.md/

Thanks in advance for any kind of help.

tl;dr class RegistrationForm extends React.PureComponent<any> {
                                      what to drop here ^^^^^                                            

You need to install the @types/redux-form package with your package manager. The @types/redux-form package includes types definitions for redux-form package.

Then you can import type definitions from redux-form , for example InjectedFormProps .

Your form that will be wrapped with reduxForm() should has props that extends InjectedFormProps<FormData = {}, P = {}> .

reduxForm() type is generic reduxForm<FormData = {}, P = {}>(...

See the example:

import * as React from 'react';
import { reduxForm, InjectedFormProps, Field } from 'redux-form';

import { IUser } from './index';

interface IProps {
  message: string;
}

class UserForm extends React.Component<InjectedFormProps<IUser, IProps> & IProps> {
  render() {
    const { pristine, submitting, reset, handleSubmit, message } = this.props;
    return (
      <form onSubmit={handleSubmit}>
        <div>{message}</div>
        <div>
          <label>First Name </label>
          <Field
            name="firstName"
            component="input"
            type="text"
            placeholder="First Name"
          />
        </div>
        <div>
          <label>Last Name </label>
          <Field
            name="lastName"
            component="input"
            type="text"
            placeholder="Last Name"
          />
        </div>
        <div>
          <button type="submit" disabled={pristine || submitting}>
            Submit
          </button>
          <button type="button" disabled={pristine || submitting} onClick={reset}>
            Clear Values
          </button>
        </div>
      </form>
    );
  }
}

export default reduxForm<IUser, IProps>({
  form: 'userForm',
})(UserForm);

编辑 Redux 表单 + Typescript 示例

The source code of @types/redux-form package is located here . You can see the types there and more complicated examples in the redux-form-tests.tsx file that is used for types checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM