简体   繁体   English

MobX在React中的单向数据流

[英]Unidirectional Data Flow in React with MobX

I'm trying to setup a project architecture using MobX and React and was wondering if doing this following would be considered "not bad". 我正在尝试使用MobX和React设置项目体系结构,并且想知道是否将以下内容视为“不错”。 I don't want this question to end up being another "this is a matter of personal preference and so this question doesn't belong here... We can all agree that some things really are bad. 我不希望这个问题最终成为另一个问题,“这是个人喜好,因此,这个问题不属于这里……我们都可以同意,有些事情确实糟糕。

So I'm thinking of only having a single Store.js file that looks something like this: 所以我在考虑只拥有一个看起来像这样的Store.js文件:

import { observable, action, useStrict } from 'mobx';

useStrict(true);

export const state = observable({
    title: ''
});

export const actions = {
    setTitle: action((title) => {
        state.title = title;
    })
};

Note : that all application state will be in state , there will only be a single store. 注意 :所有应用程序状态都将处于state ,只有一个存储。

I then use state in my root component aka App.js like so: 然后,在根组件aka App.js使用stateApp.js所示:

import React, { Component } from 'react';
import { observer } from 'mobx-react';
import { state } from './Store';
import DisplayTitle from './components/DisplayTitle/DisplayTitle';
import SetTitle from './components/SetTitle/SetTitle';

class App extends Component {

  render() {
    return (
      <div className="App">
        <DisplayTitle title={state.title}/>
        <SetTitle />
      </div>
    );
  }

}

export default observer(App);

I'll obviously have a whole bunch of components in my app, but none of the components will ever read state directly from Store.js . 很明显,我的应用程序中会包含很多组件,但是没有一个组件会直接从Store.js读取state Only my App.js will import the state and pass it down the component tree. 只有我的App.js会导入状态并将其传递到组件树下。

Another note : I'm not so sure anymore why other components can't read the state directly from Store.js ... 另一个注意事项 :我不太确定为什么其他组件不能直接从Store.js读取状态...

This is the case with the DisplayTitle component: DisplayTitle组件就是这种情况:

import React, { Component } from 'react';

class DisplayTitle extends Component {
    render () {
        return (
            <h1>{this.props.title}</h1>
        );
    }
}

export default DisplayTitle;

But, even though no other components can directly import state (except App.js ), any component can import actions from Store.js in order to mutate the state . 但是,即使没有其他组件可以直接导入stateApp.js除外),任何组件都可以Store.js导入actions以使state发生突变。

For example in the SetTitle component: 例如在SetTitle组件中:

import React, { Component } from 'react';
import { actions } from './../../Store';

class SetTitle extends Component {

    updateTitle (e) {
        actions.setTitle(e.currentTarget.value);
    }

    render () {
        return (
            <input onChange={this.updateTitle} type='text'/>
        );
    }
}

export default SetTitle;

Are there any flaws or other obvious reasons why this approach wouldn't be the best route to go? 是否有任何缺陷或其他明显的原因导致这种方法不是最佳选择? I'd love any and all feedback! 我希望收到所有反馈!

you are missing a few things 你缺少一些东西

at root level: 在根级别:

import { Provider } from 'mobx-react'
...

<Provider state={state}>
  <Other stuff />
</Provider>

At component level: 在组件级别:

import { inject } from 'mobx-react'

@inject('state')
class Foo ... {
  handleClick(){
    this.props.state.setTitle('foo')
  }
  render(){
    return <div onClick={() => this.handleClick()}>{this.props.state.title}</div>
  }
}

You can stick only the interface actions={actions} in your provider and inject that, ensuring children can call your API methods to mutate state and have it flow from bottom up. 您只能在提供程序中粘贴接口actions={actions}并将其注入,以确保子代可以调用您的API方法来改变状态并使状态自下而上流动。 Though if you were to mutate it direct, no side effects will happen because all components willReact and update in your render tree - flux is cleaner to reason about. 尽管如果直接对其进行突变,则不会发生任何副作用,因为所有组件都将在渲染树中进行willReact和更新-通量更willReact推理。

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

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