这个 MobX 语法有什么作用?它在哪里记录?

What does this MobX syntax do? Where is it documented?

最新的 MobX 6.0.1 版本有一个语法出现在 https://codesandbox.io/s/minimal-observer-p9ti4 的示例应用程序中,我不明白。

密码是:

const TimerView = observer(({ timer }: { timer: Timer }) => (
  <span>Seconds passed: {timer.secondsPassed}</span>
));

语法function({arg1,arg2})我看过并理解,但不是 function({arg1: {arg1: arg2}}) 而且由于 MobX 文档没有帮助;我在这里问。

我试过的

所以我查找了 MobX 观察者的文档,发现 https://mobx.js.org/api.html#observer-1 没有帮助。它说:

observer

Usage: observer(component)

A higher order component you can use to make a functional or class based React component re-render when observables change.

我正在更多地浏览网站,但希望这个问题和即将发布的答案能帮助其他人。

这是说您的 observer 回调有一个带 timer 属性 的对象参数。 timer 属性 的类型是 Timer.

然后解构对象将timer属性提取到变量timer.

这样想

interface Timer {
  secondsPassed: number
}
interface Component {
  timer: Timer
}
const TimerView = observer((obj: Component) => (
  <span>Seconds passed: {obj.timer.secondsPassed}</span>
));