首页 > 解决方案 > 如何在 mobx 商店,react-native 中启用严格模式

问题描述

如何在我的 react-native 地图中为我的 mobx 商店启用严格模式,我使用 mobx 商店以及反应上下文 API 和钩子。在阅读文档严格模式后,我只能更改操作中的可观察状态。

/contexts/index.js

import React from 'react'
import { ThemeStore }  from '../stores/ThemeStore.js'
import { PostStore }  from '../stores/PostStore.js'

export const storesContext = React.createContext({
  postStore: new PostStore(),
  themeStore: new ThemeStore(),
})

/stores/PostStore.js


import { observable, computed, action, flow } from "mobx";
import axios from 'axios';

export class PostStore
{
    
    
    @observable postMessage = 'Nothing to see here';
    @observable post = {};
    @observable posts = [];
    @observable pagination = {};
    @observable postCount = 0;


    @computed get visiblePosts()
    {
        return this.posts.filter( post => post.isVisible);
    };


}

标签: javascriptreactjsreact-native

解决方案


如果我理解正确的话,你想在你的 mobx 商店中强制执行严格模式。那个命令是

import { configure } from 'mobx';

configure({ enforceActions: "observed" })

这将不允许您修改外部操作的状态。设置时,它会在活动的 mobx 实例上设置全局行为。

你可以在这里找到更多信息: https ://mobx.js.org/refguide/api.html#configure


推荐阅读