How to trigger INPUT FILE event REACTJS by another DOM

后端 未结 6 1006
-上瘾入骨i
-上瘾入骨i 2021-01-02 01:53

I have a INPUT BUTTON and INPUT FILE, I want to click the BUTTON and it will trigger the INPUT FILE event

6条回答
  •  醉酒成梦
    2021-01-02 02:18

    Using Hooks with useref:

    import React, {useRef} from 'react';
    const FancyInput = () => {
        const fileInput = useRef(null)
    
        const handleClick = () => {
            fileInput.current.click()
        }
    
        const handleFileChange = event => {
            console.log("Make something")
        }
    
        return(
            
    handleFileChange(e)} ref={fileInput} />
    handleClick()}>
    ) } export default FancyInput;

提交回复
热议问题