React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)
How to programmatically trigger click event in React?
mid LevelCode ExampleCLIHooks
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.
sql
This can be done in two steps:
1. Create ref in render method:
jsx
<input ref={(input) => (this.inputElement = input)} />
csharp
2. Apply click event in your event handler:
javascript
this.inputElement.click();