【问题标题】:React Final Form Error: Must specify either a render prop反应最终形式错误:必须指定渲染道具
【发布时间】:2020-12-31 20:13:32
【问题描述】:

我正在尝试使用 React-Final-Form 构建一个简单的表单,如下所示:

import * as React from "react";
import {
  PrimaryButton,
} from "office-ui-fabric-react/lib/Button";
import { Form , Field } from "react-final-form";
import { FORM_ERROR } from "final-form";
import { IUserFormValues } from "../../models/user";
import { RootStoreContext } from "../../stores/rootStore";
import TextInputNew from "./TextInputNew";

const NewUIForm = () => {
  const rootStore = React.useContext(RootStoreContext);
  const { login } = rootStore.userStore;
  return (
    <Form
      onSubmit={(values: IUserFormValues) =>
        login(values).catch((error) => ({
          [FORM_ERROR]: error,
        }))
      }

      render={({
        handleSubmit,

      }) => (
        <Form onSubmit={handleSubmit}>
          <Field name="email" component={TextInputNew} />
          <Field name="email" component={TextInputNew} />
          <PrimaryButton type='submit' text="Save" />
        </Form>
      )}
    />
  );
};
export default NewUIForm;

TextInputNew 组件是这样的:

import * as React from "react";
import { TextField } from "office-ui-fabric-react/lib/TextField";
import { FieldRenderProps } from "react-final-form";

interface IProps extends FieldRenderProps<string, HTMLInputElement> {}

const TextInputNew: React.FC<IProps> = ({ input }) => {
  return (
    <div>
      <input {...input} />
      <TextField label="Standard" />
    </div>
  );
};
export default TextInputNew;

然后我在使用这个NewUIForm组件时遇到了这个错误

错误:必须将渲染道具、渲染函数指定为子级或组件道具到 ReactFinalForm

顺便说一下,UI框架是Fluent-UI 谁能帮我?谢谢!!

【问题讨论】:

    标签: javascript reactjs typescript react-final-form fluent-ui


    【解决方案1】:

    你第二个&lt;Form&gt;应该是&lt;form&gt;

        <form onSubmit={handleSubmit}>
          <Field name="email" component={TextInputNew} />
          <Field name="email" component={TextInputNew} />
          <PrimaryButton type='submit' text="Save" />
        </form>
    

    【讨论】:

    • 谢谢你的回答,但是在那之后,每个字段我都会有2个输入字段,我想一个来自,另一个来自,你知道怎么做解决这个问题?
    • 您必须将{...input} 传递到TextField 并删除&lt;input&gt;
    【解决方案2】:

    对于可能遇到此模糊错误消息的其他任何人,问题是

    的渲染功能出现问题。

    对于 OP,它在

    内使用了错误的表单标签。 对我来说,这是 组件 (components={MyComponent} 上的一个拼写错误的属性,哎呀)。

    由于错误可能是由多种原因引起的,并且消息不是很具体,因此可以通过浏览器调试器了解问题可能出在哪里,在我的例子中是这样的:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2022-01-22
      • 2020-04-05
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多