当我在从 react 类返回的输入上设置 autofocus 属性时,会生成以下错误:
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'.
最佳答案
react Camel 案例 attributes 。用:
<input type="text" onChange={this.handleChange} value={this.state.text} autoFocus />
关于html - 如何在 react 正在渲染的输入上设置自动对焦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33414892/