1

How can I output that productName inside that return? The items are from the selected cart items. The const order (function) does show in the console, but I can't get it to show inside the return of render.

const mapState = ({ user }) => ({
  currentUser: user.currentUser,
});

const mapCartItems = createStructuredSelector({
  items: selectCartItems,
});

const CheckingOut = (product) => {
  const { total, cartCount, items } = useSelector(mapCartItems);
    const order = {
    orderItems: items.map((item) => {
      const { productName, qty} = item;
      return {
        productName,
      };
      console.log(item.productName); // this one is showing in the console
    }),
  };

  return (
    <div>
      <Container fixed>
          <table >
            <tr>
              <td>Name : </td> // like show the items here
              <td></td>
            </tr>
            <br></br>
            <tr>
              <td>
                <PaymentIcon
                  style={{ marginRight: "1rem", color: " #e31837" }}
                />
                Total Price:
              </td>
              <td>₱{total}.00</td>
            </tr>
          </table>
        //just other codes for submit 
      </Container>
    </div>
  );
};

export default CheckingOut;
3
  • @AjeetShah not yet, I could see the products in the console. The return part is still the problem Commented Apr 6, 2021 at 1:33
  • @AjeetShah i've already edited the code Commented Apr 6, 2021 at 3:00
  • @AjeetShah I'm sorry, i'm just new to SO, you mean, I should edit the post? Commented Apr 6, 2021 at 4:44

4 Answers 4

2

Assuming items is an array similar to:

[
  { ID: 1, productName: 'p1', qty: 10 },
  { ID: 2, productName: 'p2', qty: 15 },
]

You can show productName and qty of each item in this array in <tr> and <td> of a table as:

{items.map((item) => (
    <tr key={item.ID}>
        <td>Name: {item.productName}</td>
        <td>{item.qty}</td>
    </tr>
))}

This will work even when items is empty array i.e. []. In case, it is null or undefined, you may do optional chaining.

It will show something like this in the table at UI:

Name: p1 10
Name: p2 15

PS: Above solution doesn't really require a separate function order as shown in your question.

Sign up to request clarification or add additional context in comments.

Comments

2
const order = (product) => {
  const order = {
    orderItems: items.map((item) => {
      const { productName, qty} = item;
      return {
        productName,
      };
    }),
  };

  order.orderItems.forEach((item) => console.log(item.productName)) // will log all productName
return order.orderItems
}

Function order will return order.orderItems which has array of objects. For example:

order :{
  orderItems: [
    {
      productName: 'car',
    },
    {
      productName: 'bike',
    }
  ]
}

To access each of product name you have to iterate throught Array nested in order.orderItems

order(product).forEach((item) => {
  console.log(item.productName) // car, bike
});

Comments

1

EDITED

// WHITHOUT STATE
const order = (product) => {
  const order = {
    orderItems: items.map((item) => {
      const { productName, qty} = item;
      return {
        productName,
      };
    })
  };
  return order; // Add this line to return your order object
};

// WITH STATE
const [myOrders, setMyOrders] = useState({});
const order = (product) => {
  setMyOrders({
    orderItems: items.map((item) => {
      const { productName, qty} = item;
      return {
        productName,
      };
    })
  });
  return //some layout;
};

Then in the return you can access like this:

// WITHOUT STATE 
return order().orderItems.map(item =>
  whatever you want to do with item.productName...
);

// WITH STATE
return myOrders.orderItems.map(item =>
  whatever code you want to add...
);

2 Comments

what if I already have existing codes in the return?like i've got a div inside there with some layouts
Then you can useState to set your orders and access them easily... something like const [myOrders, setMyOrders] = useState({});, check in my edited answer what it would be like
1

You can map over arrays and return each element of the array inside a jsx element. If you had an array:

const arr = [1,2,3,4,5]

You could map over them and return them as:

arr?.map(num => (<p> {num} </p>))

Likewise, you can do:

return(
  order?.orderItems?.map(item => (
    <p>{item.productName} </p>
  ))
)

5 Comments

What does the question mark mean in this context? I only know it in ternary if-else conditions.
I guess it mean order && order.orderItems && order.orderItems.map(item => ... )
Exactly as @Aga said. It doesn't procede if the preceding variable is falsy
No? optional chaining is its name. I described what it does.
Who are you talking to? OP?

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.