ReactJS- Directory selection dialog not working
我想在我的React App中创建一个目录选择对话框。我遵循了这样的SO线程,该线程可能对某些人有用,但对我而言不起作用。
编译时错误为
Property 'directory' does not exist on type 'DetailedHTMLProps
'.
我升级后对最新的RC版本17.rc.1做出了反应,认为可能对此进行了错误修复,但未成功。
编辑
@ Scratch'N'Purr在注释中建议使用hack将该脚本添加到文件末尾,使用标签进行目录选择。
1 2 3 4 5 6 7 | declare module 'react' { interface HTMLAttributes< T > extends AriaAttributes, DOMAttributes< T > { // extends React's HTMLAttributes directory?: string; webkitdirectory?:string; } } |
它在Javascript中可以正常工作,但问题出在Typescript上。猜猜,您认为这是一个问题是正确的。
不过,您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import * as React from"react"; import"./styles.css"; export default function App() { const ref = React.useRef<HTMLInputElement>(null); React.useEffect(() => { if (ref.current !== null) { ref.current.setAttribute("directory",""); ref.current.setAttribute("webkitdirectory",""); } }, [ref]); return <input type="file" ref={ref} />; } |