Не удалось получить автозапуск mobx, реакция, когда повторно срабатывает более одного раза

У меня есть простой компонент, который отображает null, но должен показывать предупреждение iOS/Android, когда shown prop из хранилища уведомлений изменяется на true, он отлично работает только один раз, используя автозапуск / реакцию / когда из mobx, и я вижу через spy, что hideNotification также был запущен правильно, чтобы установить shown обратно в false, но я больше не могу повторно запускать оповещение.

Составная часть

import { Component } from "react";
import { observer, inject } from "mobx-react/native";
import { reaction } from "mobx";
import { Alert } from "react-native";

@inject("notification")
@observer
class Notification extends Component {
  // -- methods ------------------------------------------------------------- //
  componentDidMount() {
    reaction(
      () => this.props.notification.shown,
      () => {
        if (this.props.notification.shown)
          Alert.alert(
            this.props.notification.type,
            this.props.notification.message,
            [
              {
                text: "Close",
                onPress: this.props.notification.hideNotification
              }
            ]
          );
      }
    );
  }

  // -- render -------------------------------------------------------------- //
  render() {
    return null;
  }
}

export default Notification;

Хранить

import { observable, action } from "mobx";

const ERROR = "notification/ERROR";
const SUCCESS = "notification/SUCCESS";

class NotificationStore {
  // -- store --------------------------------------------------------------- //
  @observable type = ERROR;
  @observable message = "";
  @observable shown = false;

  // -- actions ------------------------------------------------------------- //
  @action
  errorNotification(message) {
    this.type = ERROR;
    this.message = message;
    this.shown = true;
  }

  @action
  successNotification(message) {
    this.type = SUCCESS;
    this.message = message;
    this.shown = true;
  }

  @action
  hideNotification() {
    this.shown = false;
  }
}

export default NotificationStore;

person Ilja    schedule 12.07.2017    source источник
comment
Вы уверены, что autorun не работает? У меня работает.   -  person Tholle    schedule 14.07.2017


Ответы (1)


И проблема заключалась в том, что функции класса не были правильно привязаны, изменяя их на

@action
  errorNotification = (message) => {
    this.type = ERROR;
    this.message = message;
    this.shown = true;
  }

Решил это для меня

person Ilja    schedule 14.07.2017