I can't figure out why the input is not updating using MobX

半城伤御伤魂 提交于 2019-12-11 06:21:42

问题


So I tried to use MobX with react but I can't figure out why the input doesn't have its value updated.

This is the code I have written so far:

@observer(['recipeStore'])
class App extends Component {
  render() {
    return (
      <input type="text" value={this.props.recipeStore.og} onChange={(e) => {this.props.recipeStore.og = e.target.value}}/>
    );
  }
}

class RecipeStore {
  @observable og;
  @observable style;

  constructor() {
    this.og = 1;
    this.style = {og_low: 1, og_high: 1, fg_low: 1, fg_high: 1, abv_low: 1, abv_high: 1};
  }

  @computed get fg() {
    if (!this.og) return '';
    return (((this.og - 1) * 0.25) + 1).toFixed(3);
  }

  @computed get abv() {
    if (!this.og) return '';
    return ((this.og - this.fg) * 131).toFixed(1);
  }
}

const recipeStore = new RecipeStore();

ReactDOM.render(
  <Provider recipeStore={recipeStore}>
    <App />
  </Provider>,
  document.getElementById('root')
);

回答1:


I found the solution. Because I saw that my store was being updated but none of the react components lifecycle were being triggered I had to look in other places.

Turns out the problem was that the @observer decorator was not properly working.

I had to change my babel.dev.js to make sure the plugin 'babel-plugin-transform-decorators-legacy' was the first in the list.

I found the hint here: https://github.com/mobxjs/mobx/issues/105#issuecomment-213356342



来源:https://stackoverflow.com/questions/39381706/i-cant-figure-out-why-the-input-is-not-updating-using-mobx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!