Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ^^^^^                                            
like image 485
Patrickkx Avatar asked Jan 22 '18 10:01

Patrickkx


People also ask

Is redux form good?

Redux Form is very easy to use and implement, we just have to wrap our form component with the HOC provided by Redux Form and we are good to go. Applying validation to the form is very easy in Redux Form, we can apply validation to all the fields as well as validations for individual fields.

What is the redux form?

redux-form is a great way of managing forms that are powered by Redux. It is a Higher-Order-Component (HOC) that uses react-redux to make sure HTML forms in React use Redux to store all of its state.


1 Answers

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

Edit Redux Form + Typescript example

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.

like image 146
Anton Novik Avatar answered Sep 18 '22 14:09

Anton Novik