Update
Since the original answer, React has mostly transitioned to functional components and hooks. The basic principles remain the same, however, only the syntax changes.
First you need to get a reference to your DOM element using the useRef hook:
const inputRef = React.useRef(null);
...
<input ref={inputRef} />
Then you just call click() on that DOM element from an event handler:
inputRef.current?.click() // or .focus() for text fields
Note: inputRef.current will point to an actual DOM element, same as what you would get using document.getElementById or similar, so the same methods and properties are available.
Full example:
function MyComponent() {
const inputRef = React.useRef(null);
const handleClick = React.useCallback(() => inputRef.current?.click(), []);
return (
<div onClick={handleClick}>
<input ref={inputRef} />
</div>
);
}
Original Answer (class component)
You could use the ref prop to acquire a reference to the underlying HTMLInputElement object through a callback, store the reference as a class property, then use that reference to later trigger a click from your event handlers using the HTMLElement.click method.
In your render method:
<input ref={input => this.inputElement = input} ... />
In your event handler:
this.inputElement.click(); // or .focus() for text fields
Full example:
class MyComponent extends React.Component {
render() {
return (
<div onClick={this.handleClick}>
<input ref={input => this.inputElement = input} />
</div>
);
}
handleClick = (e) => {
this.inputElement.click();
}
}
Note the ES6 arrow function that provides the correct lexical scope for this in the callback. Also note, that the object you acquire this way is an object akin to what you would acquire using document.getElementById, i.e. the actual DOM-node.
inputelement, which is likely what the asker wants to accomplish with the programmatically triggered click.