skip to Main Content

Redux toolkits view:

enter image description here

I have some statements in Redux toolkits and I can’t enter some of them.
I already tried this version, but it gives me error.

  const orderDetails = useSelector((state) => state.orderDetails);
  const { order, loading, error } = orderDetails;

And then in return:

 <p>Shipping: {order.shippingAddress.country}</p>

It gives this error:

TypeError: Cannot read properties of undefined (reading 'country')

2

Answers


  1. Try accessing your data using optional chaining it will help you solving TypeErrors like this :

    <p>Shipping: {order?.shippingAddress?.country}</p>
    
    Login or Signup to reply.
  2. Going by your screenshot, order has a data property that is an array, that then contains shippingAddress – and I assume that order is sometimes undefined.

    You would be accessing

    order.data[0].shippingAddress.country

    and to prevent errors if any of those are undefined, you’d do

    order?.data?.[0]?.shippingAddress.country

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search