How to focus a Textfield after clicking a button. I tried to use autoFocus but it did not work out: Example sandbox
You need to use a ref, see https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
focusTextInput() {
// Explicitly focus the text input using the raw DOM API
// Note: we're accessing "current" to get the DOM node
this.textInput.current.focus();
}
render() {
// tell React that we want to associate the ref
// with the `textInput` that we created in the constructor
return (
);
}
}
Updated ref to inputRef for Material-UI v3.6.1.