Reduction form Typescript Skip user details

I am trying to pass custom details to my component that is decorated reduxForm. I am also very new to Typescript.

The first problem is that I cannot wrap the decorated component with a connection:

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

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

This is clearly caused by the wrong types, but, as I said, I'm new to Typescript, and I'm not sure how to handle these long errors. At this point, I do not need to pass any support to the form function validate, but it will be very useful for future tasks.

The main problem is that I cannot pass the user details to the decorated component:

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

props of the form:

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 gives one more error:

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

, , InjectedFormProps, - . , mapStateToProps. , - reduxForm.

+4
2

@types/redux-form 7.0.10 redux-form 7.1.2:

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

:

import MyForm from './MyForm';
<MyForm onSubmit={() => console.log("submit")} loading />
+7

, :

import { FormProps, reduxForm } from "redux-form";

interface InitialValuesProps {
  name: string;
}

interface CustomFormProps extends FormProps<InitialValuesProps, {}, {}> {
  loading: boolean;
}

class CustomForm extends React.Component<CustomFormProps> {
   render() {
      const loading = this.props.loading 
      const name = this.props.initialValues.name;
   }
}

export default reduxForm({ form: 'myForm' })(CustomForm)
+3

Source: https://habr.com/ru/post/1684957/


All Articles