How to select all text in input with Reactjs, when it focused? How to select all text in input with Reactjs, when it focused? javascript javascript

How to select all text in input with Reactjs, when it focused?


Functional component:

const handleFocus = (event) => event.target.select();const Input = (props) => <input type="text" value="Some something" onFocus={handleFocus} />

ES6 class component:

class Input extends React.Component {    handleFocus = (event) => event.target.select();    render() {        return (            <input type="text" value="Some something" onFocus={this.handleFocus} />        );    }}

React.createClass:

React.createClass({    handleFocus: function(event) {      event.target.select();    },    render: function() {      return (        <input type="text" value="Some something" onFocus={this.handleFocus} />      );    },})


Thanks, I appreciate it. I did it so:

var input = self.refs.value.getDOMNode();            input.focus();            input.setSelectionRange(0, input.value.length);


Another Way Functional Component with useRefHook:

const inputEl = useRef(null);function handleFocus() {  inputEl.current.select();}<input  type="number"  value={quantity}  ref={inputEl}  onChange={e => setQuantityHandler(e.target.value)}  onFocus={handleFocus}/>