Javascript 一旦我设置了screenProps的设置状态,如何避免在tabNavigator中重新呈现屏幕?
我在一个react原生项目中实现推送通知,在这个项目中,我需要从应用程序的父组件通过react导航屏幕道具为BottomTabNavigator传递参数。一旦收到通知,并更改状态以更新参数,该参数将指示必须更新的子组件,应用程序将进行完整的重新呈现。 这是我的代码:Javascript 一旦我设置了screenProps的设置状态,如何避免在tabNavigator中重新呈现屏幕?,javascript,react-native,react-navigation,react-native-firebase,Javascript,React Native,React Navigation,React Native Firebase,我在一个react原生项目中实现推送通知,在这个项目中,我需要从应用程序的父组件通过react导航屏幕道具为BottomTabNavigator传递参数。一旦收到通知,并更改状态以更新参数,该参数将指示必须更新的子组件,应用程序将进行完整的重新呈现。 这是我的代码: class AppLayout extends Component { state = { updatePushNotifications: false } handleActivatePushNotifica
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
}}
/>
);
}
}
类AppLayout扩展组件{
状态={
updatePushNotifications:false
}
handleActivatePushNotifications=()=>{
this.handlePushNotificationMessageListener();
}
handlePushNotificationMessageListener=async()=>{
this.notificationListener=firebase.notifications().onNotification((通知)=>{
const{title,body}=通知;
控制台日志(通知);
log('notificationListener');
//设置子组件中更新调用的状态
这是我的国家({
updatePushNotifications:true
});
showarert(标题、正文);
});
}
showAlert=(标题、消息)=>{
警惕,警惕(
标题
消息
[
{text:'OK',onPress:()=>console.log('OK Pressed')},
],
{可取消:false},
);
}
//执行setState后,将重新呈现整个导航组件
渲染(){
返回(
);
}
}
当我更新通过屏幕道具传递的任何参数时,如何防止重新呈现应用程序?
提前感谢您提供的任何帮助如果您在渲染方法中使用状态,它将始终使用状态更改重新渲染您的应用程序。您可以在类中定义变量并对其进行更新。然后把它传给道具。但要小心,因为如果不进行forceUpdate,您将看不到更改。如果在渲染方法中使用状态,它将始终使用状态更改重新渲染应用程序。您可以在类中定义变量并对其进行更新。然后把它传给道具。但是要小心,因为如果不进行forceUpdate,就看不到更改