首页 > 解决方案 > 在输入元素上使用自动对焦时出错(Reactjs)

问题描述

我有以下元素

<textarea className='title' autofocus="autoFocus" 
   onChange={(e)=>changeBill(e, 'title')} 
   value={bill?.title} 
   placeholder="Bill Title">
</textarea>

当我打开浏览器时,它会给出以下警告:

警告:无效的 DOM 属性autofocus。你的意思是autoFocus

它仍在工作,但警告非常烦人。我试过“自动对焦”和“自动对焦”都不能阻止警告。

标签: htmlreactjs

解决方案


警告不是关于属性的值,而是属性名称本身。它应该是:

<textarea className='title' autoFocus={true} 
   onChange={(e)=>changeBill(e, 'title')} 
   value={bill?.title} 
   placeholder="Bill Title">
</textarea>

该物业也接受truefalse。在您的示例中,非空字符串被解释为true.


推荐阅读