无法获取 mobx 自动运行、反应、何时重新触发多次
Unable to get mobx autorun, reaction, when to re-trigger more than one time
我有一个呈现 null 的简单组件,但是当来自通知存储的 shown
属性更改为 true 时应该显示 iOS/Android 警报,使用自动运行/反应/从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;
问题是 class 函数没有正确绑定,将它们更改为
@action
errorNotification = (message) => {
this.type = ERROR;
this.message = message;
this.shown = true;
}
帮我解决了这个问题
我有一个呈现 null 的简单组件,但是当来自通知存储的 shown
属性更改为 true 时应该显示 iOS/Android 警报,使用自动运行/反应/从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;
问题是 class 函数没有正确绑定,将它们更改为
@action
errorNotification = (message) => {
this.type = ERROR;
this.message = message;
this.shown = true;
}
帮我解决了这个问题