首页 > 解决方案 > 尝试设置输入属性时反应useRef返回未定义的错误

问题描述

我已经使用了来自 react 的 useRef 钩子

const myInput = useRef()

并将其传递给 material-ui OutlinedInput 组件

<OutlinedInput
  id="outlined-adornment-amount"                                      
  inputRef={myInput}
  type="file"                                            
  startAdornment={<InputAdornment position="start">
                   <FolderIcon fill="#333" />
                    </InputAdornment>}
                      labelWidth={60}
    />

当我尝试在 useEffect 中设置输入的属性时

  useEffect(() => {
        myInput.current.directory = true
    }, [])

我明白了

未捕获的类型错误:无法设置未定义的属性“目录”

标签: reactjsreact-hooksuse-ref

解决方案


我看不出你的代码有任何问题。我测试过,一切正常。看看这个:

https://codesandbox.io/s/hardcore-kare-u5rgm


推荐阅读