When setting the initial state of a component, using data passed in via props, should I create a new object, doing something like...
getInitialState: function () {
return {
fieldData: JSON.parse(JSON.stringify(this.props.data))
};
}
or is it safe to just do...
getInitialState: function () {
return {
fieldData: this.props.data
};
}
Transferring props to the component's state is considered a bad practice: http://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html
It might be best to consider a different approach. You can access props directly and the component will update when the props are changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With