0

I am new to react and was trying to use a map function inside jsx to render an array. However nothing gets rendered inside the loop .

I am passing data to my child component like this:

                            {showMaterialConfirmModal && (
                            <MaterialModalConfirm
                              closeModal={setshowMaterialConfirmModal}
                              orderList={orderListE}
                              itemList={itemListE}
                              errorList={errorListE}
                              title="Success"
                            />
                          )}

and inside the child component I am calling the map function like this:

              <Card>
              <GridContainer>
                <GridItem xs={12}>Design Successful for: 0</GridItem>
                <h5>Order:{props.orderList[0]}</h5>
                {props.orderList.map((order, i) => {
                  <div>
                    {order}
                    <h1>Hi</h1>
                    {/* <GridItem xs={12}>
                      order/item no {order[i]}/{props.itemList[i]} due to{" "}
                      {props.errorList[i]}
                    </GridItem> */}
                  </div>;
                })}
              </GridContainer>
            </Card>

The data in orderList is coming in the tag however nothing gets printed which is inside the loop.

I have checked various documents to run the map function however I am at a loss as to why nothing is getting printed .

Please help

1
  • 2
    Replace (order, i) => { ... } with (order, i) => ( ... ) Commented Jan 7, 2022 at 3:40

1 Answer 1

3

I think you are missing a return here:

{props.orderList.map((order, i) => {
    return (
      <div>
        {order}
        <h1>Hi</h1>
      </div>);
  })}

or

{props.orderList.map((order, i) => (
      <div>
        {order}
        <h1>Hi</h1>
      </div>
  ))
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using it inside the the jsx should I use return there?
Yeah it's how the map function works. It takes a function that transforms each element in the input using the function passed to map, which means the function passed to map needs to return something. Otherwise every element in the input gets transformed(mapped) into nothing. Or you can use the syntax @Phil commented, which is equivalent to what I wrote above.

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.