'How to access a part of the Redux store which contains the same name?

I want to get the commented loadingStatus from userReducer. How to achieve this?

const mapStateToProps = (state) => {
  const {
    loadingStatus,
    vehicleDetails
  } = state.vehicleReducer;
  const { 
    //loadingStatus,
    userDetails
  } = state.userReducer;
  return {
    loadingStatus,
    vehicleDetails,
    userDetails
  };
};


Solution 1:[1]

I haven't had an idea about object destructuring when asking this question. When destructuring objects, the properties of the object can be pulled out and assigned to new variables (There is no effect on the original object). Also, an appropriate name can be given for these new variables.

Example

There is a vehicle object which consists of a model, make, and year;

const vehicle = {
  model: "ABC",
  make: "XYZ",
  year: 1980
}

I want to pull out the model make and year. Also, I want to assign the make property into a variable called "manufacturer". It can be achieved by the below code snippet.

const { model, make : manufacturer, year } = vehicle;

So, the solution for question asked can be achived by below code snippet,

const mapStateToProps = (state) => {
  const {
    loadingStatus,
    vehicleDetails
  } = state.vehicleReducer;
  const { 
    loadingStatus: userLoadingStatus,
    userDetails
  } = state.userReducer;
  return {
    loadingStatus,
    vehicleDetails,
    userLoadingStatus,
    userDetails
  };
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DevLaka