简体   繁体   English

redux 表单元素类型无效:应为字符串(对于内置组件)

[英]redux form Element type is invalid: expected a string (for built-in components)

  • In my stepper code Iam trying to include the radio button.在我的步进器代码中,我试图包含单选按钮。
  • When I include the radio button I am getting the below error.当我包含单选按钮时,出现以下错误。
  • I looked into this example https://redux-form.com/6.0.0-alpha.6/examples/simple/ and then imeplemented我查看了这个例子https://redux-form.com/6.0.0-alpha.6/examples/simple/然后 imeplemented
  • but still I am faccing errors.但我仍然面临错误。
  • Can you tell me how to fix it, so that in future I will fix it myself.你能告诉我如何修复它,以便将来我自己修复它。
  • Providing my sandbox and code snippet below.在下面提供我的沙箱和代码片段。

https://codesandbox.io/s/0prxxxvy0n https://codesandbox.io/s/0prxxxvy0n

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。 You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

const AsyncValidationForm = props => {
  console.log("AsyncValidationForm ---->");

  const { handleSubmit, pristine, reset, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <Field
        name="username"
        type="text"
        component={renderField}
        label="Username"
      />
      <Field
        name="password"
        type="password"
        component={renderField}
        label="Password"
      />
      <Field name="sex" type="radio" value="male" />

      <div>
        <button type="submit" disabled={submitting}>
          Sign Up
        </button>
        <button type="button" disabled={pristine || submitting} onClick={reset}>
          Clear Values
        </button>
      </div>
    </form>
  );
};

You simply need to add the renderField component to your radio field:您只需将 renderField 组件添加到您的无线电场:

 <Field component={renderField} name="sex" type="radio" value="male" />

Why does it solve your issue?为什么它可以解决您的问题? What you are using here, is the Field component of redux-form.你在这里使用的是 redux-form 的 Field 组件。 (See your import import { Field, reduxForm } from "redux-form"; ) When you don't know how a component that you are integrating is working, there is always hope for a good documentation. (请参阅您的 import import { Field, reduxForm } from "redux-form"; )当您不知道正在集成的组件如何工作时,总是希望有一个好的文档。 In case of redux-form we are lucky: there actually is one that as well describes their Field component.在 redux-form 的情况下,我们很幸运:实际上有一个也描述了他们的 Field 组件。

What we can get there is the following:我们可以得到的是以下内容:

  1. The name prop is required.名称道具是必需的。 It is a string path, in dot-and-bracket notation, corresponding to a value in the form values.它是一个字符串路径,采用点和括号表示法,对应于形式 values 中的值。 It may be as simple as 'firstName' or as complicated as contact.billing.address[2].phones[1].areaCode.它可能像“firstName”一样简单,也可能像contact.billing.address[2].phones[1].areaCode 一样复杂。

  2. The component prop is required.组件道具是必需的。 It may be a Component, a stateless function, or a factory, like those provided under React.DOM.它可能是一个组件、一个无状态函数或一个工厂,就像 React.DOM 下提供的那些。 See Usage section below.请参阅下面的使用部分。 To learn about the props that will provided to whatever your component is, see the Props section below.要了解将提供给您的任何组件的道具,请参阅下面的道具部分。

  3. All other props will be passed along to the element generated by the component prop.所有其他道具都将传递给组件道具生成的元素。

The second paragraph explains why your approach did not work out: the component prop is required.第二段解释了为什么您的方法不起作用:需要组件道具。 :) :)

Source: https://redux-form.com/6.0.0-alpha.6/docs/api/field.md/来源: https : //redux-form.com/6.0.0-alpha.6/docs/api/field.md/

Hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误-元素类型无效:预期为字符串(对于内置组件) - ERROR - Element type is invalid: expected a string (for built-in components) React Native,元素类型无效:需要一个字符串(对于内置组件) - React Native, element type is invalid: expected a string (for built-in components) mdbreact 错误 - 元素类型无效:需要一个字符串(对于内置组件) - mdbreact error - Element type is invalid: expected a string (for built-in components) 错误:元素类型无效:需要一个字符串(对于内置组件)-reactjs - Error: Element type is invalid: expected a string (for built-in components) - reactjs TypeScript + React:元素类型无效:预期为字符串(对于内置组件) - TypeScript + React: Element type is invalid: expected a string (for built-in components) 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义 - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件) - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) 控制台错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Console error: Element type is invalid:expected a string (for built-in components) or a class/function (for composite components) but got: undefined ReactDOM - 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - ReactDOM - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined React - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - React - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM