I follow a tutorial and I know that componentWillReceiveProps will soon be out from React, so I tried myself to replace it with static getDerivedStateFromProps;
So I have a form where I can add a user profile and then edit that profile. With componentWillReceiveProps I get the data in edit profile page and I can edit it, but with getDerivedStateFromProps I only get it, but I can't type anything in the input fields.
Can anyone tell me why?
State:
state = {
displaySocialInputs: false,
handle: '',
company: '',
website: '',
location: '',
skills: '',
errors: {},
};
componentWillReceiveProps:
componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({ errors: nextProps.errors });
}
if (nextProps.profile.profile) {
console.log(nextProps.profile.profile)
const profile = nextProps.profile.profile;
// Bring skills array back to CSV
const skillsCSV = profile.skills.join(',');
// If profile field doesnt exist, make empty string
profile.company = !isEmpty(profile.company) ? profile.company : '';
profile.website = !isEmpty(profile.website) ? profile.website : '';
profile.location = !isEmpty(profile.location) ? profile.location : '';
profile.skills= !isEmpty(profile.skills) ? profile.skills : '';
// Set component fields state
this.setState({
handle: profile.handle,
company: profile.company,
website: profile.website,
location: profile.location,
skills: skillsCSV,
});
}
}
and static getDerivedStateFromProps, the one that I put it instead of the above one:
static getDerivedStateFromProps(nextProps, prevState) {
// if there is an error
if (nextProps.errors !== prevState.errors) {
// then add it to errors object
return { errors: nextProps.errors };
}
if (nextProps.profile.profile) {
console.log(nextProps.profile.profile)
const profile = nextProps.profile.profile;
// Bring skills array back into CSV
const skillsCSV = profile.skills.join(',');
// If profile field doesn't exist, make empty string
// if is not empty, then use it, else add ''
profile.company = !isEmpty(profile.company) ? profile.company : '';
profile.website = !isEmpty(profile.website) ? profile.website : '';
profile.location = !isEmpty(profile.location) ? profile.location : '';
profile.skills = !isEmpty(profile.skills) ? profile.skills : '';
// Set component fields state
return {
handle: profile.handle,
company: profile.company,
website: profile.website,
location: profile.location,
skills: skillsCSV
}
}
return null;
};
I searched a lot on internet and I saw that with getDerivedStateFromProps you neeed componentDidUpdate:
componentDidUpdate(prevProps, prevState) {
if (prevProps.errors !== this.props.errors) {
this.setState({ errors: this.props.errors })
}
if (prevProps.profile !== this.props.profile) {
const profile = this.props.profile.profile;
// Bring skills array back into CSV
// join back into a string by comma
const skillsCSV = profile.skills.join(',');
// If profile field doesn't exist, make empty string
// if is not empty, then use it, else add ''
profile.company = !isEmpty(profile.company) ? profile.company : '';
profile.website = !isEmpty(profile.website) ? profile.website : '';
profile.location = !isEmpty(profile.location) ? profile.location : '';
profile.status = !isEmpty(profile.status) ? profile.status : '';
profile.skills = !isEmpty(profile.skills) ? profile.skills : '';
// Set component fields state
this.setState({
handle: profile.handle,
company: profile.company,
website: profile.website,
location: profile.location,
status: profile.status,
skills: skillsCSV
});
}
}