与React状态很容易设置新值,这是呈现某些输入的条件,然后使用状态回调将焦点集中到该输入。Mobx和React。可观察到的变化后如何处理焦点输入?
handleToggleInput() {
const showInput = !this.state.showInput
this.setState({ showInput },() => showInput && this.refs.input.focus())
}
render() {
if(this.state.showInput) {
<input ref="input" type="text />
}
}
现在,当切换到Mobx这是不可能
@observable showInput = false;
handleToggleInput() {
this.showInput = !this.showInput
this.showInput && this.refs.input.focus()
}
render() {
if(this.showInput) {
<input ref="input" type="text />
}
}
这将失败,因为输入做出反应还没有把重新呈现组件。 有什么办法可以等待和检测什么时候恢复?
非常有趣。如果你[**推迟焦点**](http://jsbin.com/kuzariquce/edit?js,output),它就可以工作。如果你没有有条件地隐藏输入,它可以毫无延迟地工作。希望MobX专业人员能够对此发出一些启发。 – Tholle
@Tholle非常聪明!这可能现在工作得很好,但是由于rerender调度,将来可能会出现React Fiber或其他React实现的问题。 – Schovi