Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux Form Typescript Pass Custom Props

I am trying to pass custom props to my component which is decorated with reduxForm. Also I am very new in Typescript.

The first problem is that I can't wrap the decorated component with connect:

export default
    connect(mapStateToProps)(
        reduxForm({
            form: 'myform'
        })(MyComponent)
    )

The error:

Error:(89, 5) TS2345:Argument of type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' is not assignable to parameter of type 'ComponentType<{ addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & DispatchPr...'.
Type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' is not assignable to type 'StatelessComponent<{ addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & Dispa...'.
Type 'DecoratedComponentClass<any, Partial<ConfigProps<any, {}>>>' provides no match for the signature '(props: { addressValue: any; isDeliveryAddress: any; customerTypeValue: any; } & DispatchProp<any> & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.

It's obviously caused by wrong types but as I said I am new in Typescript and I am not sure how to deal with these long errors. At this moment I don't need to pass any props to the validate form function but it will be very helpful for future tasks.

The main problem is that I can't pass custom props to the decorated component:

export default reduxForm({
    form: 'myform'
})(
    connect(mapStateToProps)(MyComponent)
);

the form props:

interface Props extends InjectedFormProps {
    onSubmit: () => void;
    // some values from the state
    loading: boolean; // the custom prop
}

and when I try this:

<MyForm loading onSubmit={this.handleSubmit} />

it throws another error:

Error:(134, 25) TS2339:Property 'loading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormInstance<any, Partial<ConfigProps<any, {}>>>> ...'.

The strange part is that I am able to pass props that are declared in the InjectedFormProps interface but I can't pass any custom props. Actually, I am able to pass any props from the mapStateToProps function. Maybe I just can't pass any custom props to the decorated component with reduxForm.

like image 709
kirpt Avatar asked Sep 02 '17 18:09

kirpt


People also ask

How do I import a prop type from redux form?

If you are a strict PropTypes completionist, redux-form exports all of these propTypes , so you may import them, like so: import { reduxForm, propTypes } from 'redux-form'; class SimpleForm extends Component { static propTypes = { ... propTypes, // other props you might be using } // ...

What is typescript in Redux?

Overview TypeScript is a typed superset of JavaScript that provides compile-time checking of source code. When used with Redux, TypeScript can help provide: Type safety for reducers, state and action creators, and UI components

How to pass props in react using TypeScript?

Passing props in React using Typescript 1 Grabbing directly from props. Lets assume you're trying to pass a series of props down to a lower component in react, you're not going to do any kind of destructuring, ... 2 Dealing with objects. ... 3 Passing the same prop down to multiple components. ...

How to make typescript happy with props?

and creating it might look something like this: To make TypeScript happy, we need to tell it what to expect on that props object. To accomplish this, we need to create an interface. The interface will contain props that we're going to be referencing and their types. For our example, we might create something like this:


1 Answers

Working solution for @types/redux-form 7.0.10 and redux-form 7.1.2:

Define form component in MyForm.tsx:

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

interface StateProps {
  valueFromState: string;
}

interface Props extends StateProps {
    loading: boolean; // the custom prop
}

const MyForm: React.StatelessComponent<Props & InjectedFormProps<{}, Props>> =
  ({handleSubmit, loading}) => (
    <form onSubmit={handleSubmit}>
      {loading && (<p>loading...</p>)}
    </form>
)

const mapStateToProps = (state: any): StateProps => ({valueFromState: "1"});

export default connect(mapStateToProps)(
  reduxForm<{}, Props>({
    form: 'myform'
  })(MyForm)
);

Then use it:

import MyForm from './MyForm';
<MyForm onSubmit={() => console.log("submit")} loading />
like image 106
Denis Isaev Avatar answered Nov 11 '22 10:11

Denis Isaev