Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EmailInput wraps an HTML `input` and adds some app-specific styling.
- const EmailInput = React.forwardRef((props, ref) => (
- <input ref={ref} {...props} type="email" className="AppEmailInput" />
- ));
- class App extends Component {
- emailRef = React.createRef();
- render() {
- return (
- <div>
- <EmailInput ref={this.emailRef} />
- <button onClick={() => this.onClickButton()}>
- Click me to focus email
- </button>
- </div>
- );
- }
- // `this.emailRef.current` points to the `input` component inside of EmailInput,
- // because EmailInput is forwarding its ref via the `React.forwardRef` callback.
- onClickButton() {
- this.emailRef.current.focus();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement