0
import React, { Component } from "react";
import {
  Button,
} from "reactstrap";

import {
  Formik,
  Form,
  Field,
} from "formik";

export class UniversalInputForm extends Component {
  constructor(props) {
    super(props);

    this.state = {
      showCategories: false,
      categoryChosen: "Please choose",
      initialValues: {
        social: {
          facebook: "this is the facebook string",
          twitter: "this is the twitter string",
        },
        textValue: "Just a string value",
      },
    };
  }
  render() {
    const {
      socketMethod,
      emitSocketEvent,
    } = this.props;

let inputFields = Object.entries(this.state.initialValues).map(
  (item, index1) => {
    if (typeof item[1] === "object") {
      // console.log(item[1]);
      Object.keys(item[1]).map((itemChild, index2) => {
        if (typeof item[1][itemChild] === "string") {
          return (
          <Field name="test" placeholder="test" className="w-100" />
          );
        }
      });
    }
    if (typeof item[1] === "string") {
      return (
        <Field name={item[1]} placeholder={item[1]} className="w-100" />
      );
    }
  }
);

return (
  <div>
    <Formik
      onSubmit={(values) => {
        console.log(values);
      }}
    >
      <Form>
     <React.Fragment>   {inputFields} </React.Fragment>
        <Button
          color="success"
          type="submit"
          onClick={() => emitSocketEvent(socketMethod)}
          className="mt-3 w-100"
        > 
          Submit
        </Button>
      </Form>
    </Formik>
  </div>
);
}
  • Console.log method works after both typeof item[1] === "string" and typeof item[1] === "object"
  • Tried wrapping inputFields in <React.Fragment>, as well as displaying straight after
  • The input field appears only for typeof item[1] === "string" line.

What is the reason for this? Could it be, that React nests the inputs and nothing gets displayed as a result?

1 Answer 1

1

object.map returns array, which is ReactNode[], this is fine.

but you have a map inside another map, which will make the return type ReactNode[][].

React can't parse multidimensional array so it won't render.

try this

let inputFields = [];
Object.values(this.state.initialValues).map(
  (item, index1) => {
    if (typeof item === "object") {
      Object.values(item).map((itemChild, index2) => {
        if (typeof itemChild === "string") {
          inputFields.push(
          <Field name="test" placeholder="test" className="w-100" />
          );
        }
      });
    }
    if (typeof item === "string") {
      inputFields.push(
        <Field name={item} placeholder={item} className="w-100" />
      );
    }
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

return Object.keys(... would also fix it but yeah. I'd also add key when creating those arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.