类型“ {}”中缺少属性“ submitAction”,但类型中必需

时间:2020-04-23 09:47:49

标签: javascript reactjs typescript redux redux-form

我正在尝试学习redux格式和打字稿,但是遇到了我无法解决的错误。

错误是:Property 'submitAction' is missing in type '{}' but required in type 'Readonly<IDispatchProps & Partial<ConfigProps<IUser, IDispatchProps, string>>>'.ts(2741)

我认为这是错误所在...

// Form.tsx

import * as React from 'react';
import { Input } from "../FormInput/FormInput";
import { Button } from "../FormButton/FormButton"
import { Validation } from "./FormValidation"
import { reduxForm, InjectedFormProps, Field } from 'redux-form';
import './formStyles.scss';

export interface IUser {
  title: string;
  description: string;
}

export interface IDispatchProps {
  submitAction:() => any;
}

const Form = (props:IDispatchProps & InjectedFormProps<IUser, IDispatchProps>) => (
    <div className="LoginFormWrapper">
      <div className="bodyWrapper">
        <Field
          id="title"
          name="title"
          placeholder="Enter title"
          type="text"
          component={Input}
        />
        <Field
          id="description"
          name="description"
          placeholder="Enter description"
          type="text"
          component={Input}
        />
        <div className="buttonWrapper">
          <Button
            text="Submit"
            onClick={() => props.submitAction()}
            isDisabled={props.invalid}
          />
        </div>
      </div>
    </div>
);

export const FormEx = reduxForm<IUser, IDispatchProps>({
  form: 'Form',
  validate: Validation,
})(Form);

此错误显示在<FormEx />

import React from 'react';
import { connect } from 'react-redux';
import { createStream } from '../../actions';
import './formStyles.scss';
import { FormEx } from './Form/From';

interface StreamCreate {
  createStream: any;
}

class StreamCreate extends React.Component<StreamCreate>  {
  render() {
    return (
      <div>
        <h3>Create</h3>
        <FormEx />
      </div>
    )
  }
}

export default connect(null, { createStream })(StreamCreate);

谢谢

1 个答案:

答案 0 :(得分:0)

您的dir /s组件具有必需的Form道具。 (在submitAction界面中定义。)

将类型为IDispatchProps的任何函数传递给() => any组件,该错误应该消失了。

例如:

<FormEx />