简体   繁体   English

MobX - 将参数传递给action作为observable以更改可观察值?

[英]MobX - Pass parameter to action as observable to change observable value?

I have a MobX store, and a react native class setup and working, but not as intended. 我有一个MobX商店,并且反应本机类设置和工作,但不是预期的。 I have a list of observable user preferences in the UserPreferencesStore like this: 我在UserPreferencesStore有一个可观察的用户首选项列表,如下所示:

class UserPreferencesStore {
    @observable userPreferences = {
        receive_upvotes_mail: 0,
        receive_answers_mail: 0,
        receive_comments_mail: 1
    }
}

In the react native class, I want to update the above observables based on onPress of TouchableHighlight like this: 在本机类中,我想基于onPress的TouchableHighlight更新上面的observable,如下所示:

<TouchableHighlight onPress={() => this.onPressPreference('receive_upvotes_mail')}>
    <Image source={require('images/Email.png')}
           style={UserPreferencesStore.userPreferences.receive_upvotes_mail == 1 && styles.active} />
</TouchableHighlight>

<TouchableHighlight onPress={() => this.onPressPreference('receive_answers_mail')}>
    <Image source={require('images/Email.png')}
           style={UserPreferencesStore.userPreferences.receive_answers_mail == 1 && styles.active} />
</TouchableHighlight>

<TouchableHighlight onPress={() => this.onPressPreference('receive_comments_mail')}>
    <Image source={require('images/Email.png')}
           style={UserPreferencesStore.userPreferences.receive_comments_mail == 1 && styles.active} />
</TouchableHighlight>

and here is the onPressPreference action to update the observables that does not work... 这是onPressPreference操作来更新不起作用的observable ...

@action onPressPreference = (preferenceName) => {
    // alert(preferenceName);
    UserPreferencesStore.userPreferences.preferenceName = UserPreferencesStore.userPreferences.preferenceName == 0 ? 1 : 0;
}

The preferenceName parameter gets passed perfectly, as the alert shows it, but it does not seem to work when "appended" here UserPreferencesStore.userPreferences.preferenceName to update the passed preference observable store value. preferenceName参数会在警报显示时完美地传递,但是当“附加”此处UserPreferencesStore.userPreferences.preferenceName以更新传递的首选项可观察商店值时,它似乎不起作用。

However, If I create 3 actions, one for each preference, and trigger them onPress, then it works and the observable updates the value correctly in the store, but it's a lot of code repeated, and I really want to get it to work with the passed parameter to a single action. 但是,如果我创建3个动作,每个首选项一个,并在onPress上触发它们,那么它可以工作,并且observable在商店中正确地更新了值,但是重复了很多代码,我真的想让它与它一起工作传递给一个动作的参数。

Any idea how to get the passed parameter to work with the single action to update the respective observable's value? 知道如何让传递的参数与单个动作一起使用以更新相应的observable的值吗?

You need to use bracket notation to access a property with variable name. 您需要使用括号表示法来访问具有变量名称的属性。 UserPreferencesStore.userPreferences[preferenceName] = UserPreferencesStore.userPreferences[preferenceName] == 0 ? 1 : 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM