Жизненный цикл компонента
Lifecycle methods
class TestInput extends React.Component {
constructor(props) {
super(props)
this.input = React.createRef()
}
componentDidMount() {
// ставим фокус в input
this.input.current.focus()
}
onBtnClickHandler = () => {
alert(this.input.current.value)
}
render() {
return (
<React.Fragment>
<input
className='test-input'
defaultValue=''
placeholder='введите значение'
ref={this.input}
/>
<button onClick={this.onBtnClickHandler}>Показать alert</button>
</React.Fragment>
)
}
}Last updated