Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why input field is not render in react js?

I am trying to parse Object to render HTML element . I am not able to show any HTML element. I think there is some return statement problem in my code

here is my code
https://codesandbox.io/s/objective-sun-i3vy4i?file=/src/App.js

export default function App() {
  const [state, setState] = useState({});
  const handleChange = (e) => {
    setState({
      [e.event.name]: e.target.value
    });
  };
  const expractCompont = (component) => {
    switch (component.type) {
      case "textfield":
        return (
          <>
            <label>{component.label}</label>
            <input type="text" name={component.id} onchange={handleChange} />
          </>
        );

      case "number":
        return (
          <>
            <label>{component.label}</label>
            <input type="number" name={component.id} onchange={handleChange} />
          </>
        );

      case "select":
        return (
          <>
            <label>{component.label}</label>
            <select name={component.id} id={component.id}>
              {component.values.map((item, index) => (
                <option value={item.value} key={index}>
                  {item.label}
                </option>
              ))}
            </select>
            <input
              type="number"
              name={component.field_107we45}
              onchange={handleChange}
            />
          </>
        );
      default:
        return <></>;
    }
  };

  const renderComponents = (components) => {
    for (let index = 0; index < components.length; index++) {
      const element = components[index];
      expractCompont(element);
    }
  };
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {renderComponents(data.components)}
    </div>
  );
}

Expected output :
Expected output to see input fields ,select box

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your renderComponents function returns nothing. You need to return an array from it.

  const renderComponents = (components) => {
    const elementsToRender = [];
    for (let index = 0; index < components.length; index++) {
      const element = components[index];
      elementsToRender.push(expractCompont(element))
    }
    return elementsToRender;
  };

Actually, the renderComponents function is not really needed, you can put everything at once in return

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {data.components.map(expractCompont)}
    </div>
  );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading