Mobx-react 没有渲染但是状态改变了
Mobx-react no render but state changed
我决定尝试 Mobx,但遇到了组件对存储库中更改的字段缺乏响应的问题。我看了类似的主题,但我仍然不明白问题是什么。如果在更改后将 属性 值打印到控制台,您会看到实际结果。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from "mobx-react";
import AppStore from "./AppStore";
import App from './App';
ReactDOM.render(
<React.StrictMode>
<Provider AppStore={AppStore}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
App.js
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
@inject('AppStore')
@observer class App extends Component {
render() {
const { AppStore } = this.props;
console.log(AppStore);
return(
<div className="App">
{ this.props.AppStore.counter }
<hr/>
<button onClick={this.props.AppStore.increment}>+</button>
<button onClick={this.props.AppStore.decrement}>-</button>
</div>
)
}
}
export default App;
AppStore.js
import { observable, action } from "mobx";
class AppStore {
@observable counter = 0;
@action increment = () => {
this.counter = this.counter + 1;
console.log(this.counter);
}
@action decrement = () => {
this.counter = this.counter - 1;
console.log(this.counter);
}
}
const store = new AppStore();
export default store;
Since mobx@6.0.0
decorators are not enough. You have to make your class observable manually with makeObservable
还有。
class AppStore {
@observable counter = 0;
constructor() {
makeObservable(this);
}
@action increment = () => {
this.counter = this.counter + 1;
}
@action decrement = () => {
this.counter = this.counter - 1;
}
}
我决定尝试 Mobx,但遇到了组件对存储库中更改的字段缺乏响应的问题。我看了类似的主题,但我仍然不明白问题是什么。如果在更改后将 属性 值打印到控制台,您会看到实际结果。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from "mobx-react";
import AppStore from "./AppStore";
import App from './App';
ReactDOM.render(
<React.StrictMode>
<Provider AppStore={AppStore}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
App.js
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
@inject('AppStore')
@observer class App extends Component {
render() {
const { AppStore } = this.props;
console.log(AppStore);
return(
<div className="App">
{ this.props.AppStore.counter }
<hr/>
<button onClick={this.props.AppStore.increment}>+</button>
<button onClick={this.props.AppStore.decrement}>-</button>
</div>
)
}
}
export default App;
AppStore.js
import { observable, action } from "mobx";
class AppStore {
@observable counter = 0;
@action increment = () => {
this.counter = this.counter + 1;
console.log(this.counter);
}
@action decrement = () => {
this.counter = this.counter - 1;
console.log(this.counter);
}
}
const store = new AppStore();
export default store;
Since mobx@6.0.0
decorators are not enough. You have to make your class observable manually with makeObservable
还有。
class AppStore {
@observable counter = 0;
constructor() {
makeObservable(this);
}
@action increment = () => {
this.counter = this.counter + 1;
}
@action decrement = () => {
this.counter = this.counter - 1;
}
}