ReactNative:五十二更新UNSAFE_componentWillReceiveProphotoshop
一、不建议使用的
UNSAFE_componentWillReceiveProps(nextProps)
UNSAFE_componentWillMount()
UNSAFE_componentWillUpdate(nextProps, nextState)
二、新增加的
getDerivedStateFromProps(nextProps, prevState)
1.父组件通知子组件pros发生变更时
2.子组件每次render前
3.首次构造时执行
getSnapshotBeforeUpdate(nextProps, prevState)
1.首次构造时不执行
2.子组件每次render后
三、UNSAFE_componentWillReceiveProps更新方案
1.由于getDerivedStateFromProps为static,所以无法调用this.props。
-
static getDerivedStateFromProps(nextProps, prevState) {
-
const {type} = nextProps;
-
// type可能由props驱动,也可能由state驱动,这样判断会导致state驱动的type被回滚,
-
// 如果子组件中设置type为不同的值,type到此依赖又被赋值为props中的值
-
if (type !== prevState.type) {
-
return {
-
type,
-
};
-
}
-
// 否则,对于state不进行任何操作
-
return null;
-
}
在state中增加prevPropEmail,prevState.prevPropEmail相当于this.props.email
-
state = {
-
email: this.props.email,
-
prevPropEmail: '',
-
};
-
-
static getDerivedStateFromProps(nextProps, prevState) {
-
if (nextProps.email !== prevState.prevPropEmail) {
-
return {
-
email: nextProps.email,
-
prevPropEmail: nextProps.email,
-
};
-
}
-
return null;
-
}
返回值即,this.setSate中的对象,return null 或 return {} ,相当于this.setState({})进行整体的刷新
另一种不同写法
-
constructor(props) {
-
super(props);
-
this.state = {
-
type: 0,
-
props,
-
}
-
}
-
static getDerivedStateFromProps(nextProps, prevState) {
-
const {type} = nextProps;
-
const {props} = prevState;
-
// 这段代码可能看起来非常混乱,这个props可以被当做缓存,仅用作判断
-
if (type !== props.type) {
-
return {
-
type,
-
props: {
-
type,
-
},
-
};
-
}
-
return null;
-
}
2.异步情况也可以由componentDidUpdate完成,如三方组件
-
// 原代码
-
UNSAFE_componentWillReceiveProps(nextProps) {
-
// Either centerOn has never been called, or it is a repeat and we should ignore it
-
if (
-
(nextProps.centerOn && !this.props.centerOn) ||
-
(nextProps.centerOn &&
-
this.props.centerOn &&
-
this.didCenterOnChange(this.props.centerOn, nextProps.centerOn))
-
) {
-
// 执行混合动画
-
this.centerOn(nextProps.centerOn);
-
}
-
}
-
public componentDidUpdate(prevProps: ImageZoomProps): void {
-
// Either centerOn has never been called, or it is a repeat and we should ignore it
-
if (
-
(this.props.centerOn && !prevProps.centerOn) ||
-
(this.props.centerOn && prevProps.centerOn && this.didCenterOnChange(prevProps.centerOn, this.props.centerOn))
-
) {
-
this.centerOn(this.props.centerOn);
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbkbje
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01