Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from "react";
- import ReactDOM from "react-dom";
- import "./styles.css";
- const thisInput = React.createRef();
- class App extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- inputVal: ""
- };
- }
- componentDidMount() {
- thisInput.current.focus();
- }
- onChangeInput() {
- this.setState({
- inputVal: thisInput.current.value
- });
- }
- render() {
- return (
- <div className="App">
- <h1>Ref Focus Input On Load</h1>
- <input
- type="text"
- ref={thisInput}
- onChange={e => this.onChangeInput(e)}
- />
- <p>Result : {this.state.inputVal}</p>
- </div>
- );
- }
- }
- const rootElement = document.getElementById("root");
- ReactDOM.render(<App />, rootElement);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement