关于javascript:ReactJS-目录选择对话框不起作用

ReactJS- Directory selection dialog not working

我想在我的React App中创建一个目录选择对话框。我遵循了这样的SO线程,该线程可能对某些人有用,但对我而言不起作用。
编译时错误为

Property 'directory' does not exist on type 'DetailedHTMLProps'.

enter

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上。猜猜,您认为这是一个问题是正确的。

不过,您可以使用ref手动设置。

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} />;
}