【发布时间】:2015-10-29 12:56:08
【问题描述】:
当我在从反应类返回的输入上设置自动对焦属性时,会生成以下错误:
error TS2339: Property 'autofocus' does not exist on type 'HTMLAttributes'
这是有问题的代码:
return <input type="text" onChange={this.handleChange} value={this.state.text} autofocus/>;
我尝试了更详细的autofocus="autofocus",但没有成功。
这可以使用 react 和 html 来完成吗?我可以用 javascript 设置它,但如果存在,我宁愿使用 html 解决方案。
(编辑)
正如 Dominic Tobias 所指出的,autoFocus(区分大小写)本身就可以解决问题,如果您尝试像这样设置属性autoFocus="autofocus",您将收到错误消息:
TS2322 Type 'string' is not assignable to type 'boolean'.
【问题讨论】:
-
我对 reactjs 不熟悉,但你不应该在 onchange 和 value 周围使用引号
value="this.state.text"吗?也许浏览器将自动对焦视为您价值的一部分。