具有反应形式的嵌套组件react.cloneElement

时间:2017-08-10 21:02:39

标签: javascript forms reactjs ecmascript-6

我想解决此库的问题:react-form。 有关信息,这是我当前的错误:

  

未捕获错误:元素类型无效:期望一个字符串(对于内置组件)或类/函数(对于复合组件)但得到:object。检查CustomInput的呈现方法。

这就是我想要做的事情:



<CustomInput field="date" showErrors={false}>
    <DatePickerForm />
</CustomInput>
&#13;
&#13;
&#13;

这是我的CustomInput组件:

&#13;
&#13;
import React, { Component } from 'react';
import FormInput from 'react-form';

class CustomInput extends Component {
  render() {
    const { showErrors, field, children } = this.props;
    return (
      <FormInput showErrors={showErrors} field={field}>
        {({ setValue }) => {
          return (
            React.cloneElement(children, { setValueForm: setValue })
          );
        }}
      </FormInput>
    );
  }
}
export default CustomInput;
&#13;
&#13;
&#13;

React.cloneElement(children,{setValueForm:setValue})似乎返回一个对象。 但是,如果我只是渲染React.cloneElement而没有&#34; FormInput&#34;部分,渲染是好的。 所以,我认为问题来自FormInput的功能。

我做错了什么?

您可以查看FormInput class

感谢。

1 个答案:

答案 0 :(得分:2)

看起来你输错了:https://www.npmjs.com/package/react-form#-forminput-

import { FormInput } from "react-form"

而不是

import FormInput from "react-form"
相关问题