In React ES6, why does the input field lose focus after typing a character?

前端 未结 10 2133
悲&欢浪女
悲&欢浪女 2020-12-08 12:54

In my component below, the input field loses focus after typing a character. While using Chrome\'s Inspector, it looks like the whole form is being re-rendered instead of ju

10条回答
  •  清歌不尽
    2020-12-08 13:23

    You have to use a unique key for the input component.

    
    

    The key="random1" cannot be randomly generated. For example,

    uuid() will generate a new set of string for each rerender. This will cause the input to lose focus.

    If the elements are generated within a .map() function, use the index to be part of the key.

    {rens.map((ren,i)=>{
        return(
        
    {ren}{i}
    ) }

    This will solve the issue.

提交回复
热议问题