我正在React本机项目中实现推送通知,其中我需要从应用程序的父组件中通过BottomTabNavigator的reactNavigation screenProps传递参数。一旦收到通知,并且更改状态以更新将指示必须更新的子组件的参数,就将对应用程序进行完整的重新呈现。这是我的代码:
class AppLayout extends Component {
state = {
updatePushNotifications: false
}
handleActivatePushNotifications = () => {
this.handlePushNotificationMessageListener();
}
handlePushNotificationMessageListener = async () => {
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
console.log(notification);
console.log('notificationListener');
//SETSTATE FOR UPDATE CALLS IN CHILD COMPONENTS
this.setState({
updatePushNotifications: true
});
this.showAlert(title, body);
});
}
showAlert = (title, message) => {
Alert.alert(
title,
message,
[
{text: 'OK', onPress: () => console.log('OK Pressed')},
],
{cancelable: false},
);
}
// The entire navigation component is re-rendered once the setState is executed
render () {
return (
<Layout
screenProps={{
updatePushNotifications: this.state.updatePushNotifications
}}
/>
);
}
}
当我更新通过screenProps传递的任何参数时,如何防止重新呈现应用程序?
提前感谢您的帮助
如果您在渲染方法中使用状态,它将始终使用状态更改来重新渲染您的应用。您可以在类中定义变量并对其进行更新。然后将其传递给道具。但是要小心,因为如果不进行forceUpdate,就看不到更改。