gpt4 book ai didi

javascript - 一旦我设置了 screenProps 的 setState,如何避免在 tabNavigator 中重新渲染屏幕?

转载 作者:行者123 更新时间:2023-11-28 03:21:47 25 4
gpt4 key购买 nike

我正在一个 React Native 项目中实现推送通知,其中我需要从应用程序的父组件通过 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 传递的任何参数时,如何防止应用程序重新渲染?

预先感谢您的帮助

最佳答案

如果您在渲染方法中使用状态,它总是会根据状态更改重新渲染您的应用程序。您可以在类中定义变量并更新它。然后将其传递给 prop。但要小心,因为如果不进行强制更新,您将看不到更改。

关于javascript - 一旦我设置了 screenProps 的 setState,如何避免在 tabNavigator 中重新渲染屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59095403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com