mobx 的 Eslint 插件,如果你在没有观察者的情况下使用商店,它会发出警告

Eslint plugin for mobx that warns if you are using the store without an observer

如果我在未包含在观察者 HOC 中的组件中使用 mobx 操作,是否存在警告的 eslint 插件:

const Comp1 = () => {
 const store = useContext(rootContext)
 return (
   number: {store.number}
   <button onClick={() => store.incByOne()}>increase number by 1</button>
 )
}

export default Comp1  // warn on console
export default observer(Comp1) // fine

不,有 none,因为 eslint 不可能真正理解您正在使用某种 observable 值。 observer 也可以用不同的方式定义,不将组件包装在一个内部并不意味着您没有在其他地方这样做。

虽然有一个 configuration option 用于 MobX:

configure({ observableRequiresReaction: true })

Warns about any unobserved observable access. Use this if you want to check whether you are using observables without a "MobX context". This is a great way to find any missing observer wrappers, for example in React components. But it will find missing actions as well.