How can I open an file input with a button click ? My code :
<Button variant="outlined">
Choose Image
</Button>
<input
type="file"
id="input_file"
accept=".jpg,.jpeg,.png"
style={{ display: 'none' }}
/>
useRef hookimport React, { useRef } from 'react'
const MyComponent = () => {
const ref = useRef()
const handleClick = (e) => {
ref.current.click()
}
return (
<>
<button onClick={handleClick}>Upload Image</button>
<input ref={ref} type="file" />
</>
)
}
export default MyComponent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With