我有一个呈现null的简单组件,但是当来自通知存储的shown
道具更改为true时,它应该显示iOS / Android警报,它只使用一次使用autorun / reaction /来自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;
答案 0 :(得分:0)
问题是类功能没有正确绑定,将它们改为
@action
errorNotification = (message) => {
this.type = ERROR;
this.message = message;
this.shown = true;
}
为我解决了这个问题