I want to focus the input box in my child component as soon as it renders and hence I am trying to pass ref of the input box up to its parent, which has a modal. And I am trying to focus on the input on entering the modal. (using the onEntered prop of react-bootstrap modal)
What am I using to create a modal? -> react-bootstrap
Parent Component's JSX:
<Modal
show={props.isLoginModalOpen}
onHide={props.toggleLoginModal}
onEntered={() => { //<----- I plan to use onEntered to focus on
this.mobileInput.focus(); // the input element in child
}}
>
<Modal.Header closeButton>
<Modal.Title></Modal.Title>
</Modal.Header>
<Modal.Body>
<EnterMobileView /> // <------- Child Component
</Modal.Body>
</Modal>
Child Component's JSX:
<div>
<Form>
<div className='form-group'>
<input
type='number'
name='mobile'
placeholder='Phone Number'
className='form-control'
ref={(input) => (this.mobileInput = input)} // <---- I want to pass this
/> // ref up to the parent
</div>
</Form>
</div>
Is this the correct way to do this? is there a better way?