如何取消对 InputNumber antd 元素的关注?

我在代码中遇到这个问题,当我尝试使用 onchange 事件更改 InputNumber 的值时,焦点会转移到 First InputNumber 元素,而不是我希望更改的位置。因此,这会导致在 InputNumber 的每次更改时滚动页面
这是我的最小代码:
Minimal Code

白衣非少年
浏览 139回答 1
1回答

郎朗坤

一种解决方案是在父组件中创建这些输入的引用,然后在发生更改时使用焦点函数。class DemoComp extends React.Component {&nbsp; constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.inputRef1 = React.createRef();&nbsp; &nbsp; this.inputRef2 = React.createRef();&nbsp; }&nbsp; getInputNumber = (e, para, inputRef) => {&nbsp; &nbsp; console.log(e, para);&nbsp; &nbsp; storeInputNumber[e] = para;&nbsp; &nbsp; inputRef.focus();&nbsp; };和&nbsp; &nbsp; &nbsp; <InputNumber&nbsp; &nbsp; &nbsp; &nbsp; id="input 2"&nbsp; &nbsp; &nbsp; &nbsp; ref={input => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.inputRef1 = input;&nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; onChange={e => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.getInputNumber(e, "input 2", this.inputRef1);&nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; />另一个输入也是如此。检查我的codesandboxhttps://codesandbox.io/s/practical-newton-918jn
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript