How to autofocus for input name based on whether this.props.email exists or not?
if(this.props.email){
// would like to set autofocus for <input value={email} name="userName">
}else{
// would like to set autofocus for <input name="password" />
}
<input value={email} name="userName">
<input name="password" />
I was thinking of using refs but is wondering if there is better way to access the name of the input
you may want to try this
<input value={email} autoFocus={this.props.email} name="userName">
This doesn't use if-else, but uses this.props.email, as in your question:
How to autofocus for input name based on whether this.props.email exists or not?
Inside Input.js (component)
<input
value={this.props.email}
name="userName"
autoFocus={this.props.email}
/>
<input name="password" autoFocus={!this.props.email} />
Inside index.js (parent)
<Input email={""} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With