问题
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