Я бы хотел включить функцию onClick, когда реагирует монтирование компонентов (рендеринг).Мне нравится использовать Javascript DOM API метод .click ().Он отлично работает в приложении реакции (без laravel-mix) и вне курса в консоли.
Что мне нужно сделать, чтобы получить ту же функциональность в приложении laravel-реагировать с laravel-mix ???
Ожидаемый результат: окно предупреждения с сообщением, на которое нажали
Фактический результат: ничего
//to fire onClick
document.getElementById("imgInput").click()
//used in pure componentDidMount
componentDidMount = () => {
document.getElementById("imgInput").click();
};
//code on fire
_handleClick =()=>{
alert('clicked');
}
<button id="imgInput" onClick={this.handleClick} type="file">
click me
</button>
//Edited with react ref
constructor(props) {
super(props);
this.imgInputRef = React.createRef();
}
componentDidMount = () => {
if ( this.imgInputRef) {
const $ref = this.imgInputRef;
$ref.current.click();
}
};
_onUploadImageHandler = e => {
// handeling image upload
};
//rendering button
<div>
<input
accept="image/*"
type="file"
multiple
id="imgInput"
ref={this.imgInputRef}
onChange={this._onUploadImageHandler}
/>
<label htmlFor="imgInput" style={{ width: "100%" }}>
<Button
htmlFor="imgInput"
variant="contained"
component="span"
size="large"
style={{ width: "100%" }}
>
<CloudUpload />
<span>{uploadBtnText}</span>
</Button>
</label>
</div>