zoukankan      html  css  js  c++  java
  • [Mobx] Use MobX actions to change and guard state

    This lesson explains how actions can be used to control and modify the state of your application. They help you to structure your code base and integrate well with the MobX React Devtools. Actions automatically create transactions, which group changes together.

    const {observable, computed} = mobx;
    const {observer} = mobxReact;
    const {Component} = React;
    const DevTools = mobxDevtools.default;
    
    
    const t = new class Temperature {
      @observable unit = "C";
      @observable temperatureCelsius = 25;
    
      @computed get temperatureKelvin() {
        console.log("calculating Kelvin")
        return this.temperatureCelsius * (9/5) + 32
      }
       
      @computed get temperatureFahrenheit() {
        console.log("calculating Fahrenheit")
        return this.temperatureCelsius + 273.15
      }
       
      @computed get temperature() {
        console.log("calculating temperature")
        switch(this.unit) {
          case "K": return this.temperatureKelvin + "ºK"
          case "F": return this.temperatureFahrenheit + "ºF"
          case "C": return this.temperatureCelsius + "ºC"
        }
      }
    }
       
    const App = observer(({ temperature }) => (
      <div>
        {temperature.temperature}
        <DevTools />
      </div>
    ))
    
    ReactDOM.render(
      <App temperature={t} />,
      document.getElementById("app")
    )

    We have @Observable and @Computed defined, once we change any @Observable value, we can get new value in the @Computed.

    But currently, the way we changing the value is though directly object mutation, such as:

    t.unit = "F"

    Of course, it is not good enough, what we can do is using @action to change the value:

    const {observable, computed, action, transaction, useStrict} = mobx;
    const {observer} = mobxReact;
    const {Component} = React;
    const DevTools = mobxDevtools.default;
    
    useStrict(true);
    
    const t = new class Temperature {
      @observable unit = "C";
      @observable temperatureCelsius = 25;
    
      @computed get temperatureKelvin() {
        console.log("calculating Kelvin")
        return this.temperatureCelsius * (9/5) + 32
      }
       
      @computed get temperatureFahrenheit() {
        console.log("calculating Fahrenheit")
        return this.temperatureCelsius + 273.15
      }
       
      @computed get temperature() {
        console.log("calculating temperature")
        switch(this.unit) {
          case "K": return this.temperatureKelvin + "ºK"
          case "F": return this.temperatureFahrenheit + "ºF"
          case "C": return this.temperatureCelsius + "ºC"
        }
      }
    
      @action setUnit(newUnit) {
        this.unit = newUnit;
      }
    
      @action setCelsius(degrees) {
        this.temperatureCelsius = degrees;
      }
       
      @action("update temperature and unit")
      setTemperatureAndUnit(degrees, unit) {
        this.setCelsius(degrees);
        this.setUnit(unit);
      }
    }
       
    const App = observer(({ temperature }) => (
      <div>
        {temperature.temperature}
        <DevTools />
      </div>
    ))
    
    ReactDOM.render(
      <App temperature={t} />,
      document.getElementById("app")
    )

    Action can be anynomous action or named action:

    @action setCelsius(degrees)
    @action("update temperature and unit") // named
  • 相关阅读:
    双11实时物流订单最佳实践
    一文理解 K8s 容器网络虚拟化
    新能源汽车太猛了,这些卡脖子技术你了解吗?
    龙蜥社区成立系统运维SIG,重磅开源sysAK系统运维工具集
    sysAK(青囊)系统运维工具集:如何实现高效自动化运维?| 龙蜥技术
    零信任策略下云上安全信息与事件管理最佳实践
    MySQL 处理重复数据
    JavaScript toFixed()、toExponential、toPrecision方法
    Nginx 安装
    spring定时任务执行两次的原因与解决方法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7499302.html
Copyright © 2011-2022 走看看