在 React Native 中存储静态和公共数据
Storing static and common data in React Native
你好,我正在 React Native 中创建一个字典应用程序,我只想存储一个 JSON blob 数组,用于保存每个单词的定义。
我非常希望避免对数据进行硬编码并希望我的代码是干的!
示例 JSON blob:
[
{
"word": "triangle",
"definition": "a plane figure with three straight sides and three angles.",
"type": "noun"
},
{
"word": "square",
"definition": "a plane figure with four equal straight sides and four right angles.",
"type": "noun"
},
{
"word": "circle",
"definition": "a round plane figure whose boundary (the circumference) consists of points equidistant from a fixed point (the center).",
"type": "noun"
}
]
存储此数据的最佳策略是什么,以便:
- 可由用户添加书签
- 干净,易于更改并与其他文件分开
- 我的 React 组件如何访问它
我认为关系数据库是最好的方法,但我很难弄清楚如何用数据为数据库播种。以及 React Native 上的哪个库用于关系数据库。
感谢您阅读我的问题。
您可以使用以下模式使用 Realm 来执行您所描述的操作:
let EntrySchema = {
name: 'Entry',
primaryKey: 'word',
properties: {
word: 'string',
definition: 'string',
type: 'string'
}
};
let BookmarkListsSchema = {
name: 'BookmarkList',
properties: {
bookmarks: {type: 'list', objectType: 'Entry'}
}
};
let realm = new Realm({schema: [EntrySchema, BookmarkListsSchema]});
您可以使用所有字典条目预填充 Realm 文件并将其与您的应用程序捆绑在一起,或者您可以下载此文件或 JSON 并在启动应用程序时初始化您的数据库。
要创建/添加书签:
// create your list once
var bookmarkList;
realm.write(() => {
bookmarkList = realm.create('BookmarkList');
});
// add entry for 'triange' to bookmarks
realm.write(() => {
let triangeEntry = realm.objectForPrimaryKey('Entry', 'triangle');
bookmarkList.bookmarks.push(triangleEntry);
});
相关文章:
- 要使用XML显示的静态数据
- 使用公共JS文档优化静态网站的CSS/JS导入
- 如何在公共js模块中从web服务返回数据
- 从ajax post与静态数据中剔除映射数据
- 如何在像骨干提线木偶这样的单页框架中使用公共的、跨域的json数据
- Twitter 使用静态数据引导类型
- 使用 Knockout.js 在文本区域中添加数据绑定和静态文本
- Yahoo api 从公共数据 JavaScript 切换到 oAuth
- 将数据加载到静态网站中的 JavaScript 以供立即使用的最快方法是什么?
- 基于KendoUI网格内的服务数据的静态下拉列表值选择
- D3.js - 来自 CSV 的数据不显示,静态数据将显示
- 如何在不泄露应用机密的情况下从客户端访问 Facebook 公共数据
- CSS效果可以很好地处理静态表数据,但当通过javascript添加表数据时就不行了
- Google BigQuery通过API访问公共数据集
- 在node.js中,应该在数据库中还是在类中使用公共数据集
- 流星JS铁路由器:共享公共'数据'之间的路线
- 在每个ajax调用中传递公共数据
- 使用jQuery从Ajax响应构建表行(不使用静态json数据)
- 在 React Native 中存储静态和公共数据
- 流星和会话/公共数据