3

When I set the autofocus attribute on an input I'm returning from a react class the following error is generated:

 error TS2339: Property 'autofocus' does not exist on type 'HTMLAttributes'

This is the offending code:

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

I've tried the more verbose autofocus="autofocus" with no luck.

Can this be done using react and html? I could set it with javascript but I'd rather a html solution if one exists.

(Edit) As pointed out by Dominic Tobias, autoFocus (case sensitive) on its own does the trick, if you try to set the property like this autoFocus="autofocus" you will get the error:

TS2322  Type 'string' is not assignable to type 'boolean'.
1
  • I'm not familiar with reactjs, but shouldn't you use quotes around the onchange and value like this value="this.state.text"? Maybe the browser sees autofocus as part of your value. Commented Oct 29, 2015 at 13:03

1 Answer 1

10

React camel-cases attributes. Use:

 <input type="text" onChange={this.handleChange} value={this.state.text} autoFocus />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.