দেখে মনে হচ্ছে componentWillReceivePropsএকটি নতুন লাইফসাইकल পদ্ধতির পক্ষে getDerivedStateFromProps: স্ট্যাটিক গেটডেরাইভস্টেটফ্রোমপ্রসেস () প্রকাশিত আসার ক্ষেত্রে সম্পূর্ণ পর্যায়ক্রমে বের হয়ে যাচ্ছে ।
পরিদর্শন করার পরে, এটা দেখে মনে হচ্ছে আপনি এখন মধ্যে একটি সরাসরি তুলনা করতে অক্ষম this.propsএবং nextProps, মত আপনি তে componentWillReceiveProps। এই কাছাকাছি কোন উপায় আছে?
এছাড়াও, এটি এখন একটি বস্তু ফেরত দেয়। আমি কী ধরে নিচ্ছি যে রিটার্নের মানটি মূলত this.setState?
নীচে আমি অনলাইনে একটি উদাহরণ পেয়েছি: প্রপস / রাজ্য থেকে প্রাপ্ত রাজ্য ।
আগে
class ExampleComponent extends React.Component {
state = {
derivedData: computeDerivedState(this.props)
};
componentWillReceiveProps(nextProps) {
if (this.props.someValue !== nextProps.someValue) {
this.setState({
derivedData: computeDerivedState(nextProps)
});
}
}
}
পরে
class ExampleComponent extends React.Component {
// Initialize state in constructor,
// Or with a property initializer.
state = {};
static getDerivedStateFromProps(nextProps, prevState) {
if (prevState.someMirroredValue !== nextProps.someValue) {
return {
derivedData: computeDerivedState(nextProps),
someMirroredValue: nextProps.someValue
};
}
// Return null to indicate no change to state.
return null;
}
}
componentWillReceiveProps