Reactjs 如何在react本机应用程序中替换组件WillReceiveProps?
我听说ComponentWillReceiveProps生命周期方法不是在react原生项目中使用的好方法,而是使用getDerivedStateFromProps。 所以我尝试用getDerivedStateFromProps替换我的组件WillReceiveProps。但我不知道该怎么做,我尝试了componentDidUpdate(),但它反复调用setstateReactjs 如何在react本机应用程序中替换组件WillReceiveProps?,reactjs,react-native,Reactjs,React Native,我听说ComponentWillReceiveProps生命周期方法不是在react原生项目中使用的好方法,而是使用getDerivedStateFromProps。 所以我尝试用getDerivedStateFromProps替换我的组件WillReceiveProps。但我不知道该怎么做,我尝试了componentDidUpdate(),但它反复调用setstate componentWillReceiveProps(nextProps) { if(nextProps.shopLi
componentWillReceiveProps(nextProps) {
if(nextProps.shopList.isFetching===false) {
this.setState({isLoading:false})
if(!_.isEqual(nextProps.shopList, this.props.shopList) && nextProps.shopList.error===false ) {
this.formatShops(nextProps.shopList.shops)
} else {
}
} else {
this.setState({isLoading:true})
}
if(nextProps.common.isFetching===false) {
this.setState({isLoading:false})
if(nextProps.common.error===false) {
if(!_.isEqual(nextProps.common, this.props.common) && nextProps.common.error===false ) {
if(nextProps.common.otpverifysucess==false) {
this.props.navigation.dispatch(logoutAction);
}
}
}
}
}
这是我的整个组件将接收道具。有人可以帮助将其移动到getDerivedStateFromProps生命周期方法中吗
componentDidUpdate
将获取prevProps值作为参数,而this.props
将具有新的props值
替换组件将通过componentdiddupdate
接收道具:
因此,如果您要将组件willreceiveprops
替换为componentdiddupdate
,请将nextrops
替换为this.props
,将this.props
替换为prevProps
nextrops
(componentWillReceiveProps的参数)到this.props
(在componentdiddupdate
中)
this.props
(内部组件diddupdate)到prevProps
(内部组件diddupdate)
请尝试以下代码:
getDerivedStateFromProps(nextProps, prevState) {
if(nextProps.shopList.isFetching === false || nextProps.common.isFetching === false) {
return { isLoading: false }
} else {
return { isLoading: true }
}
}
componentDidUpdate(prevProps) {
if(this.props.shopList.isFetching === false) {
if(!_.isEqual(this.props.shopList, prevProps.shopList) && this.props.shopList.error === false ) {
this.formatShops(this.props.shopList.shops)
}
}
if(this.props.common.error === false && this.props.common.isFetching === false) {
if(!_.isEqual(this.props.common, prevProps.common) && this.props.common.error === false) {
if(this.props.common.otpverifysucess == false) {
this.props.navigation.dispatch(logoutAction);
}
}
}
}