简体   繁体   English

Reactjs - 从数据集构建表单,FormElement 不返回渲染函数

[英]Reactjs - Building form from data set, FormElement not returning to the render function

I am dynamically building a form based on the state build in the constructor.我正在根据构造函数中的状态构建动态构建表单。 I am having success building the outer html but the inner form html is not rendering.我成功构建了外部 html,但内部表单 html 没有呈现。 cAN SOMEONE POINT OUT WHAT i AM DOING WRONG HERE?有人能指出我在这里做错了什么吗?

class Forms extends Component {
    constructor(props) {
        super(props);
        this.state = {
            enrollment: {
                class: "form-style",
                fieldsets: [{
                    id: "1",
                    title: "Company Enrollment Form",
                    formElements: [{
                        label: "Company Name:",
                        element: "input",
                        type: "text",
                        class: "",
                        name: "cName",
                        placeholder: "Your Company's Name *",
                        required: true,
                        disabled: false
                    }, {
                        label: "Company Type:",
                        element: "select",
                        type: "populateDDL",
                        class: "",
                        name: "sltCompanyType",
                        placeholder: "",
                        required: true,
                        disabled: false
                    }]
                }]
            }
        }
    }

    render() {
        let Content = null;
        if (this.props.type === "enrollment") {
            Content = <EnrollmentForm state={this.state.enrollment} />
        } else if (this.props.type === "contact") {
            Content = <ContactUsForm />
        } else {
            Content = <fourOhFour />
        }
        return (
            <div className="container formContent">
                {Content}
            </div>
        );
    }
};

function EnrollmentForm(form) {
    function renderFieldsets(fieldsets) {
        if (fieldsets.length > 0) {
            return fieldsets.map((fieldset, index) => (
                <Fieldset key={index} set={fieldset} />
            ));
        }
        else return [];
    }
    function renderFormElements(formElements) {
        if (formElements.length > 0) {
            return formElements.map((formElement, i) => (
                <FormElement key={i} set={formElement} />
            ));
        }
        else return [];
    }
    const FormElement = (props, index) => {
        console.log(props);
        /* ^^^ NOT APPEARING/LOGGING IN THE CONSOLE ^^^ */
        if (props.tag === "input") {
            return (
                <input key={index} name={props.name} />
            );
        }else if (props.tag === "select") {
            return (
                <select key={index} />
            );
        }
    };
    const Fieldset = (props, index) => {
        const elements = renderFormElements(props.set.formElements);
        return (
            <fieldset key={index}>
                <legend>
                    <span className="number fa fa-address-card"></span>
                    {props.set.title}
                </legend>
            </fieldset>
        );
    };
    const fieldsets = renderFieldsets(form.state.fieldsets);
    return (
        <form className={form.state.class}>
            {fieldsets}
        </form>
    );
}

The FormElement variable is not returning the html... I need to return a different type of html element based on what the tag is in the data model. FormElement 变量没有返回 html...我需要根据数据模型中的标记返回不同类型的 html 元素。 In the code I have successfully created the outer fieldset but when I go to insert the html elements inside the fieldset, it doesn't do anything.在代码中,我成功创建了外部字段集,但是当我在字段集内插入 html 元素时,它什么也没做。 I put a console.log in the code block but apparently even when I try to use the "FormElement" the code isn't firing ...我在代码块中放了一个 console.log 但显然即使当我尝试使用“FormElement”时,代码也没有触发......

You have bugs in your code.您的代码中有错误。

Your FormElement should look like你的FormElement应该看起来像

const FormElement = (props, index) => {
      console.log(props);
      /* ^^^ NOT APPEARING/LOGGING IN THE CONSOLE ^^^ */
      if (props.set.element === "input") {//changed from props.tags--  1
          return (
              <input key={index} name={props.name} />
          );
      }else if (props.set.element === "select") {//changed from props.tags --1
          return (
              <select key={index} />
          );
      }
      return <div>Something which is not select or input</div>; //added a fallback return. --1
  };

The bugs that were fixed above are:上面修复的错误是:

(1) props.tags changed to props.set.element . (1) props.tags改为props.set.element There was no tags component in the props.道具中没有标签组件。 So none of the if blocks were being rendered and this led to the component not returning anything.所以没有一个 if 块被渲染,这导致组件没有返回任何东西。 This led to an error that said这导致了一个错误,说

FormElement(...): A valid React element (or null) must be returned. FormElement(...):必须返回有效的 React 元素(或 null)。 You may have returned undefined, an array or some other invalid object.您可能返回了 undefined、数组或其他一些无效对象。

This was resolved by adding the fallback return statement (2).这是通过添加回退返回语句 (2) 解决的。

Your FieldSets should look like你的FieldSets应该看起来像

const Fieldset = (props, index) => {
      const elements = renderFormElements(props.set.formElements);
      return (
          <fieldset key={index}>
              <legend>
                  <span className="number fa fa-address-card"></span>
                  {props.set.title}
              </legend>
              <div>{elements}</div> // consumed the elements that was created in renderFormElements.
          </fieldset>
      );
  };

The elements variable was never used in render, which led to no FormElements showing up. elements变量从未在渲染中使用过,这导致没有出现 FormElements。 That was the only error fixed in the above code.这是上面代码中修复的唯一错误。

I would suggest cleaning up your code by我建议通过以下方式清理您的代码

  1. Converting EnrollmentForm to a class.EnrollmentForm转换为类。
  2. Moving FieldSet and FormElement to seperate classes or at least funtional componentsFieldSetFormElement移动到单独的类或至少是功能组件

That would make the logic a lot easier to debug in future.这将使逻辑在未来更容易调试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM