I'm creating and returning this object with properties that originates in a deeply nested object:
const mapStateToProps = (state) => ({
firstName: state.currentUser.obj.firstName,
lastName: state.currentUser.obj.lastName,
email: state.currentUser.obj.email,
})
Another way of doing it is like this:
const mapStateToProps = (state) => {
const {
obj: {firstName, lastName, email},
} = state.currentUser;
return {
firstName,
lastName,
email,
};
}
I'm pretty happy with the first example, but it would be nice to finde a way to skip the repetition of state.currentUSer.obj for every line.
Is there any conciser way to accomplish the above than the given examples?
Input object:
const state = {
currentUser: {
isUpdatingAccount: false,
isUpdatingFavouriteColor: false,
obj: {
firstName: null,
lastName: null,
email: null,
favoriteColor: null,
favoriteTeam: null,
},
},
}
const {firstName, lastName, email} = state.currentUser.obj;