问题描述
我是 React Native 的新手,我正在创建一个面向内容的应用程序.我正在尝试做一些非常简单的事情,即:在我的应用程序中包含一些静态数据/资源,我可以在我的应用程序中读取/加载它们.在桌面应用程序中,就像安装一些本地 *.csv 文件,在应用程序中打开和读取它们一样简单,但我在 React Native 环境中找不到这样做的方法.
I'm new to React Native and I'm creating a content-oriented app. I'm trying to do something pretty simple which is: include some static data/resources in my app, which I can read/load in my application. In a desktop application, it would be as simple as installing some local *.csv files, opening and reading them within the application, but I can't find the way to do that in the React Native environment.
根据我的研究,似乎人们通常使用 json 格式的数据创建源 (.js) 文件,但这会很尴尬,因为例如我的应用程序使用了很多表格,而 .csv 表示会更好.
From my research it seems people usually create source (.js) files with data in json format, but that would be awkard because my app uses a lot of tables for example, and the .csv representation would be way better.
有什么方法可以存储非 json 或 .js 源文件中的读取静态资产/数据?
Is there any way to store read static assets/data which are not json or in .js source files?
推荐答案
获取文件路径使用
react-native-fs
获取文件路径.
使用yarn add papaparse
import Papa from "papaparse";
Papa.parse(file-path, {
complete: function(results) {
console.log(results);
}
});
如果 CSV 有标题
Papa.parse(file-path, {
header: true
complete: function(results) {
console.log(results);
}
});
所以它会将每个项目呈现为数组形式的对象
so it will render each item as objects in array form
这篇关于如何在 React Native/Expo 中存储/读取静态内容数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!