1

I have an array of object data about a form:

const formFields = [
      {
        "label": "Current Password",
        "type": "password",
        "name": "currentPassword",
        "placeholder": "Current Password",
        "required": true
      },
      {
        "label": "New Password",
        "type": "password",
        "name": "newPassword1",
        "placeholder": "New Password",
        "required": true
      },
      {
        "label": "Confirm New Password",
        "type": "password",
        "name": "newPassword2",
        "placeholder": "Confirm New Password",
        "required": true
      },
    ]

I have the component that use the array:

return (
        <Form> 
            {
                formFields.map(function(data) {
                    return 
                    <Form.Group className="mb-3">
                        <Form.Label>{ data["label"] }</Form.Label>
                        <Form.Control type={ data["type"] } name={ data["name"] } placeholder={ data["placeholder"] }  required={ data["required"] } />
                    </Form.Group>;
                }) 
            }
        
            <Button variant="primary" type="submit">
                Submit
            </Button>
        </Form>
    )

All i see on the page is a button. No form label and form fields. Supposedly, the map function should create an array of element from the array of object formFields. However, it is not doing it, and the color of the return statement within in it is light, like it's never executed.

3
  • Because if return is on its own line, it will think you want to return undefined. So you never come to the Form.Group part Commented Oct 6, 2022 at 13:48
  • 1
    is it thing of React or a thing in JavaScript? Commented Oct 6, 2022 at 13:53
  • 2
    @Rongeegee It's JavaScript. Because "return" is a complete statement, when JavaScript sees a carriage return, it assumes that the next line begins a new statement. By putting a curly brace, or a parenthesis, you tell the parser that return ( continues on the next line. Commented Oct 6, 2022 at 14:37

2 Answers 2

2

Try wrap your return from the map function in parenthesis or put it on the same line as the <Form>, currently every iteration of your map is returning void which will cause nothing to render.

  return (
    <Form>
      {formFields.map(function (data) {
        return (
          <Form.Group className="mb-3">
            <Form.Label>{data["label"]}</Form.Label>
            <Form.Control
              type={data["type"]}
              name={data["name"]}
              placeholder={data["placeholder"]}
              required={data["required"]}
            />
          </Form.Group>
        );
      })}

      <Button variant="primary" type="submit">
        Submit
      </Button>
    </Form>
  );
Sign up to request clarification or add additional context in comments.

4 Comments

the "return" and "(" have to be on the same line?
Yeah, a return by itself will always just return void without the "("
is that a thing of JavaScript or a thing in React? @Chris
It's a Javascript thing, it's the same thing as straight up returning undefined. React will take an undefined or null variable without throwing any errors and render nothing which can lead to confusion.
1

This is why semicolons, parentheses and curly braces are used in JavaScript, and why you see nothing; it is one of the ugly part of the language! This is what your mapping function does :

formFields.map(function(data) {
  return   // i.e. the function returns undefined here
  // code below not evaluated
  <Form.Group className="mb-3">
    <Form.Label>{ data["label"] }</Form.Label>
    <Form.Control type={ data["type"] } name={ data["name"] } placeholder={ data["placeholder"] }  required={ data["required"] } />
  </Form.Group>;
});

Change it to either :

formFields.map(function(data) {
  return (
    <Form.Group className="mb-3">
      <Form.Label>{ data["label"] }</Form.Label>
      <Form.Control type={ data["type"] } name={ data["name"] } placeholder={ data["placeholder"] }  required={ data["required"] } />
    </Form.Group>
  );
})

Or, better yet, use the arrow function syntax :

formFields.map(data => (
  <Form.Group className="mb-3">
    <Form.Label>{ data["label"] }</Form.Label>
    <Form.Control type={ data["type"] } name={ data["name"] } placeholder={ data["placeholder"] }  required={ data["required"] } />
  </Form.Group>
))

Comments

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.