I ran into this problem, .focus() only works with setTimeout if I select this and stop working. can someone explain to me what is the reason for this, maybe i am doing it wrong and how i can fix this problem.
componentDidMount() { React.findDOMNode(this.refs.titleInput).getElementsByTagName('input')[0].focus(); }
the setTimeout example works
componentDidMount() { setTimeout(() => { React.findDOMNode(this.refs.titleInput).getElementsByTagName('input')[0].focus(); }, 1); }
Jxs
<input ref="titleInput" type="text" />
and I followed this example React focus on input after rendering
rendering function
render() { const {title, description, tagtext, siteName} = (this.state.selected !== undefined) ? this.state.selected : {}; const hasContentChangedYet = this.hasContentChangedYet(title, description); return ( <div> <h2 className={styles.formMainHeader}>Edit Meta-Data Form</h2> <table className={styles.formBlock}> <tbody> <tr> <td className={styles.tagEditLabel}> Tag </td> <td className={styles.inputFieldDisableContainer}> {tagtext} </td> </tr> <tr> <td className={styles.tagEditLabel}> Site </td> <td className={styles.inputFieldDisableContainer}> {siteName} </td> </tr> <tr> <td className={styles.tagEditLabel}> Title </td> <td className={styles.inputFieldContainer}> <ReactInputField ref="titleInput" id="title" defaultValue={(title) ? title : ''} onChange={this.onInputChange} placeholder="Title" clearTool={true} /> </td> </tr> <tr> <td className={styles.tagEditLabel}> Description </td> <td className={styles.inputFieldContainer}> <ReactInputField id="description" defaultValue={(description) ? description : ''} onChange={this.onInputChange} placeholder="Description" clearTool={true} /> </td> </tr> </tbody> </table> <div className={styles.formFooter}> <button id="save-button" className={styles.saveButton} disabled={!hasContentChangedYet} onClick={() => this.handleSavePressed()}> Save </button> <button id="form-cancel-button" className={styles.cancelButton} onClick={this.actions.form.cancelUpdateToTagData}> Cancel </button> </div> </div> ); }
Bruce source share