В моем приложении под getDerivedStateFromProps я должен написать 2 таких условия.
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;
}
Но здесь только первое, если условие работает. Как мне организовать этот код?
Что значит работает только первое условие? Похоже, вы хотите, чтобы они оба работали одновременно?





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;
}
что я думаю, только сначала, если условие работает, потому что у вас есть оператор возврата в этом условии.