【问题标题】:How can I set autofocus on an input that react is rendering如何在反应正在渲染的输入上设置自动对焦
【发布时间】: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" 吗?也许浏览器将自动对焦视为您价值的一部分。

标签: html reactjs autofocus


【解决方案1】:

反应骆驼案例attributes。使用:

 <input type="text" onChange={this.handleChange} value={this.state.text} autoFocus />

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 2020-10-07
    • 2019-02-21
    • 1970-01-01
    • 2013-09-29
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多