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

时间:2019-11-28 19:48:55

标签: javascript react-native react-navigation react-native-firebase

我正在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传递的任何参数时,如何防止重新渲染应用程序?

在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果在渲染方法中使用状态,它将始终使用状态更改重新渲染应用。您可以在类中定义一个变量并对其进行更新。然后将其传递给道具。但是要小心,因为如果不进行forceUpdate,就看不到更改。