我一直在使用计算出的mobx,但是我无法理解以下代码正在发生什么:
import React, { Component } from "react";
import { render } from "react-dom";
import { observable, computed } from "mobx";
import { observer } from "mobx-react";
@observer
class Counter extends Component {
@observable count = 0;
@observable secondCount = 0;
@computed
get countString() {
console.log("countString Computed");
return `${this.count} ${this.secondCount}`;
}
onChangeCount = () => {
this.count = this.count + 1;
this.secondCount = this.secondCount + 1;
};
render() {
console.log("render Counter");
return (
<div>
<p>Count String: {this.countString}</p>
<button onClick={this.onChangeCount}>Change count</button>
</div>
);
}
}
render(<Counter />, document.getElementById("root"));
当用户按下更改计数btn时,输出为:
countString Computed
render Counter
countString Computed
为什么不这样:
countString Computed
countString Computed
render Counter
或类似这样:
render Counter
countString Computed
countString Computed
总而言之,在上面的代码段中如何确定日志的执行顺序?