React.js - input losing focus when rerendering

后端 未结 19 2030
猫巷女王i
猫巷女王i 2020-12-05 01:49

I am just writing to text input and in onChange event i call setState, so React rerenders my UI. The problem is that the text input always lose a f

19条回答
  •  天命终不由人
    2020-12-05 02:20

    My answer is similar to what @z5h said.

    In my case, I used Math.random() to generate a unique key for the component.

    I thought the key is only used for triggering a rerender for that particular component rather than re-rendering all the components in that array (I return an array of components in my code). I didn't know it is used for restoring the state after rerendering.

    Removing that did the job for me.

提交回复
热议问题