Reputation: 9457
In my application, under getDerivedStateFromProps, I have to write 2 conditions like this.
static getDerivedStateFromProps(nextProps) {
if (nextProps.errors) {
return { errors: nextProps.errors };
}
if (nextProps.profile.profile) {
const profile = nextProps.profile.profile;
profile.company = !isEmpty(profile.company) ? profile.company : '';
profile.website = !isEmpty(profile.website) ? profile.website : '';
profile.location = !isEmpty(profile.location) ? profile.location : '';
return {
company: profile.company,
website: profile.website,
location: profile.location,
};
}
return null;
}
But, here only the first if condition works. How should I organize this code?
Upvotes: 0
Views: 111
Reputation: 462
static getDerivedStateFromProps(nextProps) {
const result = {};
if (nextProps.errors) {
result.errors = nextProps.errors;
}
if (nextProps.profile.profile) {
const profile = nextProps.profile.profile;
profile.company = !isEmpty(profile.company) ? profile.company : '';
profile.website = !isEmpty(profile.website) ? profile.website : '';
profile.location = !isEmpty(profile.location) ? profile.location : '';
result.company = profile.company;
result.website = profile.website;
result.location = profile.location;
}
return result;
}
Upvotes: 1