在React中读取文件夹和文件可以使用Node.js的内置fs模块。fs模块提供了读取文件夹和文件的方法,例如:
const fs = require("fs");
// 读取文件夹
fs.readdir("folder_path", (err, files) => {
if (err) throw err;
console.log(files);
// 读取文件
fs.readFile("file_path", "utf-8", (err, data) => {
if (err) throw err;
console.log(data);
请注意,fs模块是一个服务端模块,不能在浏览器中直接使用。因此,您需要使用Webpack或Browserify等工具将其打包到客户端代码中。