标签: mobx

mobx:array.map()不是一个函数

mobx我使用以下方式创建了一个商店:

import {extendObservable} from 'mobx';

class InfluencerFeedStore {
    constructor() {
        extendObservable(this, {
            data: []
        });
    }

    setData(items = []) {
        this.data = items;
    }
}

export default new InfluencerFeedStore();
Run Code Online (Sandbox Code Playgroud)

然后我在我的 React 视图中观察该商店:

import React from 'react';
import {observer} from 'mobx-react';
import FeedItem from './FeedItem';
import InfluencerFeedStore from '../../core/stores/InfluencerFeed';

import './style.css';

const generateItems = () => {
    return InfluencerFeedStore.data.map((item, i) => (
        <FeedItem key={`feeditem-${i}`} {...item} />
    ));
};

const Feed = () => (
    <div className="Feed vertical-scroll-flex-child">
        {generateItems()}
    </div>
); …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs mobx mobx-react

5
推荐指数
1
解决办法
3362
查看次数

@observable 在 MobX 的底层是如何工作的?

可观察到的现象在幕后到底是什么样子的?属性如何变得可观察?这到底意味着什么?找不到不使用 ES.next 装饰器的明确解释。谢谢!

javascript mobx

5
推荐指数
1
解决办法
964
查看次数

在 React 组件之外访问 MobX 状态?

我正在使用 MobX 存储来保存一些用户身份验证数据作为可观察数据。我想访问一些我想在组件的注入/观察者模式之外运行的函数的数据。这样做明智吗?

例如,身份验证函数如下:

function authMe() { ...access mobx data here to perform conditional logic}
Run Code Online (Sandbox Code Playgroud)

reactjs mobx mobx-react

5
推荐指数
1
解决办法
3673
查看次数

MobX,以及商店之间的连接

伙计们。\n我正在开发 ract+mobx+firebase 应用程序。\n我想将我的应用程序逻辑分为 3 个商店:

\n\n
    \n
  • authStore - 存储,所有 Firebase 身份验证操作都发生在其中
  • \n
  • userStore - 存储来自 firebase 数据库的所有当前用户数据的位置
  • \n
  • edtorStore - 编辑器组件中发生的所有内容。
  • \n
\n\n

因此,要从 db 接收 currentUser 数据,我首先需要从fb.auth()获取currentUser.uid。\n我的AuthStore如下所示:

\n\n
class AuthStore {\n    @observable auth = {\n        authUser  : null,\n        authError : null\n    };\n\n    constructor () {\n        console.log ( \'start auth\' );\n        this.unwatchAuth = Fb.auth.onAuthStateChanged ( user => {\n            console.log ( \'status changed\' );\n            this.auth.authUser = user;\n        } );\n    }\n\n    @computed\n    get currentUser () {\n …
Run Code Online (Sandbox Code Playgroud)

javascript firebase reactjs mobx mobx-react

5
推荐指数
1
解决办法
2879
查看次数

MobX 自动运行和构造函数内的反应

自动运行反应必须在构造函数内部才能工作吗?我可以在没有构造函数的情况下编写这个简单的示例吗?

另外,我在自动运行中的代码可以正常运行,但如果我将其更改为console.log(this.expenses)它就不起作用。这是为什么?

import { observable, action, computed, useStrict, autorun, reaction } from 'mobx'
useStrict(true)

class ExpensesStore {

  @observable.shallow expenses = []

  @action addExpense = (expense) => {
   this.expenses.push(expense)
  }

  @computed get getExpense() {
    if(this.expenses.length > 0) {
      return `This is computed from ${this.expenses[0] + this.expenses[1]}`
    }

  }


  constructor() {
    autorun(() => {
      console.log(`${this.expenses}`)
    })

    reaction(
      ()=>this.expenses.map(expense => expense), expense => console.log(expense)
    )
  }


}

const store = window.store= new ExpensesStore() …
Run Code Online (Sandbox Code Playgroud)

javascript autorun reactjs mobx

5
推荐指数
1
解决办法
6491
查看次数

Mobx doesn't observe classes in observable array

If I push a class instance into an observable array in MobX then it is not observed. However if I push a literal object into an observable array then it will be observed.

The docs for observable arrays say that

"all (future) values of the array will also be observable"

so I am trying to understand why this happens.

For example the following code can be run in node:

let mobx = require('mobx');

class TodoStore {
  constructor() {
    this.todos = …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs mobx

5
推荐指数
1
解决办法
1482
查看次数

使用 react native i18n 更改 react native 应用程序中的语言不起作用

当用户选择语言时,我试图在运行时更改我的应用程序的语言。我正在尝试这样做

import I18n from 'react-native-i18n'

onChangeLanguage = () => {
    console.log('String before change : >>> ', I18n.t('welcome'))
    I18n.locale = 'hi'
    console.log('String after change : >>> ', I18n.t('welcome'))
}
Run Code Online (Sandbox Code Playgroud)

在两个日志中,我得到了相同的 string 。我从这里参考。在此参考中,有一个使用 redux 的示例,但我的用例是使用 mobx。如果语言更改了如何重新渲染组件以反映语言更改。

android react-native mobx react-native-android react-native-ios

5
推荐指数
0
解决办法
1621
查看次数

如何在 React Native 中持久化 Mobx 状态树?

我需要在 React Native 中保留一个 MST Store。数据很少更改。

我对使用 AsyncStorage 和 AutoRun 感到困惑。

react-native mobx mobx-state-tree

5
推荐指数
1
解决办法
2124
查看次数

在 React 应用程序中从商店更改路线

我有一个 React 组件,它接收用户名和密码并将其发送以进行身份​​验证。如果身份验证成功,页面应该移动到呈现另一个组件的不同路由。

现在的问题是,我不确定如何从我的商店更改路线?即不使用<Link>React Router的组件?我知道我们可以使用this.props.history.push(/url)以编程方式更改路由,但它只能在 React 组件中使用 using <Route>,而不能在商店中使用。

我使用 React16 和 MobX 进行状态管理,使用 ReactRouter V4 进行路由。

店铺:

class Store {
    handleSubmit = (username, password) => {
        const result = validateUser(username, password);
        // if result === true, change route to '/search' to render Search component
    }
}
Run Code Online (Sandbox Code Playgroud)

登录组件:

const Login = observer(({ store }) => {
    return (
        <div>
           <form onSubmit={store.handleSubmit}>
                <label htmlFor="username">User Name</label>
                <input type="text" id="username" onChange={e => store.updateUsername(e)} value={store.username} />
                <label htmlFor="password">Password</label>
                <input …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-router mobx

5
推荐指数
1
解决办法
1151
查看次数

在我的项目中使用 mobx 时是否有问题,错误是“当前未启用对实验语法 'decorators-legacy' 的支持”

我配置了 package.json 和 babelrc,但是在使用装饰器时出现了一些错误。

我使用“react16.8.1”和“mobx5.9.4”。

我的 babelrc 代码:

    "presets": [
        "@babel/preset-env",
        "@babel/preset-react",

    ],
    "plugins": [
        "@babel/plugin-transform-runtime",
        "@babel/plugin-transform-object-assign",
        [
           "@babel/plugin-decorators-legacy",
            {
          "legacy": true
            }
        ],


    ]
}```
and my package.json

Run Code Online (Sandbox Code Playgroud)
{
  "name": "material-dashboard-pro-react",
  "version": "1.5.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "3.9.2",
    "@material-ui/icons": "3.0.2",
    "axios": "^0.18.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "chartist": "0.10.1",
    "history": "4.7.2",
    "mobx": "^5.9.4",
    "mobx-react": "^5.4.3",
    "moment": "2.24.0",
    "moment-jalaali": "^0.8.3",
    "node-sass": "4.11.0",
    "nouislider": "13.1.0",
    "perfect-scrollbar": "1.4.0",
    "rc-pagination": "^1.17.13",
    "react": "16.8.1",
    "react-big-calendar": "0.20.3",
    "react-bootstrap-sweetalert": "4.4.1",
    "react-chartist": "0.13.3",
    "react-datetime": "2.16.3",
    "react-dom": "16.8.1",
    "react-google-maps": "9.4.5", …
Run Code Online (Sandbox Code Playgroud)

reactjs mobx mobx-react

5
推荐指数
0
解决办法
1902
查看次数