React:无法从 src 文件夹中获取本地文件,但可以从 public

React: Can't fetch local file from src folder, but can from public

我正在尝试获取本地文件“file.html”并将其设置为字符串状态。该程序无法在 src 文件夹中找到我的文件,而是在 public 中。如何在 src.

const [html, setHTML] = useState("");

async function getProjectCode() {
    const file = "file.html"
    await fetch(file)
    .then((response) => response.text())
    .then((data) => {
        setHTML(data);
    });
} 

对于 html 文件的静态导入,您可以使用 webpack HTML webpack plugin。 如果它们是动态内容 (ajax),您必须将文件从 src 文件夹移动到 public.