简体   繁体   English

MobX本地状态与全局状态

[英]MobX local state vs global state

I'm using React with mobx to handle application state. 我正在使用React和mobx来处理应用程序状态。

I'm using dump components which alter data through the external store (ajax call, filter or map array etc ...) 我正在使用转储组件,这些组件通过外部存储(ajax调用,过滤器或映射数组等)来更改数据。

Instead in forms, when you have to handle input change through onChange event, currently I'm using observable and action inside the component itself. 相反,在表单中,当您必须通过onChange事件处理输入更改时,当前我正在组件本身内部使用observable和action。

Is it a bad practice? 这是不好的做法吗? Or should I put all the actions and observable data inside an external state? 还是应该将所有操作和可观察的数据置于外部状态内?

If instead, this practice is acceptable, how can I address the case in which I have to reset a local observable state (like text input) in reaction to ajax call action executed in an external store? 如果相反,这种做法是可以接受的,那么我该如何解决因响应外部存储中执行的ajax调用操作而不得不重置本地可观察状态(例如文本输入)的情况? Could i use callback in action store to give up the control to an action inside the component, like in the following example: 我可以在操作存储中使用回调来放弃对组件内部操作的控制,如以下示例所示:

import React from 'react';
import { observer, inject } from "mobx-react";
import { observable, action } from "mobx";

@inject("rootStore")
@observer
class ContactForm extends React.Component {

constructor(props) {
  super(props);
  this.externaStore = this.props.rootStore.contactStore
  this.onChangeInput = this.onChangeInput.bind(this)
}

@observable text = ''

@action
onChangeInput(event) {
  this.text = event.target.value
}

@action
resetForm() {
  this.text = ''
}

render() {
 return (
  <form>
    <div className="form-group">
      <label htmlFor="input-text" className="control-label">Text input: 
      </label>
      <input onChange={this.onChangeInput} type="text" value={this.text} id="input-text" name="input-text" className="form-control" />
   </div>

    <button onClick={() => this.externaStore.submitForm(this.text, this.resetForm)} className="btn btn-danger btn-xs" type="button" title="Aggiungi">
      <i className="fa fa-save"></i> Aggiungi
    </button>
   </form>
  )
 }
}

class ExternalStore {

constructor(rootStore) {
  this.rootStore = rootStore
  this.service = rootStore.service
}

@observable textList = []

@action
submitForm(text, cb) {
  this.service.doAjax('POST', '/api/text', JSON.stringify(text)).then(data => {
   this.textList.push(text)
   cb()
  })
 }
}

Are there another best practice to handle similar cases? 是否有另一种最佳做法来处理类似案件?

I think the general answer is that MobX (or Redux for that matter) isn't the right place for form state. 我认为一般的答案是MobX(或Redux)不是表单状态的正确选择。 This is for many reasons, but mainly for performance, complexity and maintainability concerns. 这有很多原因,但主要是出于性能,复杂性和可维护性的考虑。 MobX/Redux is for global application state, whereas form state is much more local. MobX / Redux用于全局应用程序状态,而表单状态则更本地化。

The community is moving towards libraries like Formik, that let you fully manage the form state and lifecycle locally within the component. 社区正朝着诸如Formik之类的库发展,这些库使您可以在组件内部本地全面管理表单状态和生命周期。 It works well with MobX/Redux too, to optionally initialise the values from global state. 它也可以与MobX / Redux配合使用,以选择从全局状态初始化值。 Check it out, it's pretty great! 检查一下,这非常棒!

https://github.com/jaredpalmer/formik https://github.com/jaredpalmer/formik

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

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