0

我想在 react-typescript 环境中创建一个反应最终形式。我收到一个错误,我在“ FormProps ”所需的 {...} 类型中缺少“ onSubmit ”属性。我检查了界面,但无法弄清楚为什么会出现此错误,因为我已经设置并定义了该属性。

我的文件.tsx

import * as React from 'react';
import { Form, Field } from 'react-final-form';

export default class MyComponent extends React.Component {
  constructor(props: any) {
    super(props);

    this.onSubmitHandler = this.onSubmitHandler.bind(this);
    this.validateHandler= this.validateHandler.bind(this);
  }

   onSubmitHandler(): void {
   }

   validateHandler(): void {

   }

  render(): React.ReactNode {
    return (
      <div>
        <Form>
          onSubmit={this.onSubmitHandler} // OK
          validate={this.validateHandler}
          render={ ( {handleSubmit, pristine, invalid} ) => (
              <form onSubmit={handleSubmit}>


                <button type="submit" disabled={pristine || invalid}>
                  Submit
                </button>
              </form>
           )}
        </Form>
      </div>
    );
  }
}

index.d.ts(反应最终形式) - FormProps => Config => ...onSubmit

export const Form: React.ComponentType<FormProps>;

export interface FormProps extends Config, RenderableProps<FormRenderProps>{
  subscription?: FormSubscription;
  decorators?: Decorator[];
  initialValuesEqual?: (a?: object, b?: object) => boolean;
}

export interface Config<FormData = object> {
  debug?: DebugFunction
  destroyOnUnregister?: boolean
  initialValues?: object
  keepDirtyOnReinitialize?: boolean
  mutators?: { [key: string]: Mutator }
  onSubmit: (
    values: FormData,
    form: FormApi,
    callback?: (errors?: object) => void
  ) => object | Promise<object | undefined> | undefined | void
  validate?: (values: object) => object | Promise<object>
  validateOnBlur?: boolean
}

在此处输入图像描述

4

1 回答 1

2

道具应在元素的开始标签内提供。将您的render方法更改为此。

render(): React.ReactNode {
  return (
    <div>
      <Form 
        onSubmit={this.onSubmitHandler} // OK
        validate={this.validateHandler}
        render={ ( {handleSubmit, pristine, invalid} ) => (
          <form onSubmit={handleSubmit}>
            <button type="submit" disabled={pristine || invalid}>
              Submit
            </button>
          </form>
        )}>  
      </Form>
    </div>
  );
}
于 2019-03-13T11:37:10.127 回答