javascript - Pattern for lookup values that support UIs (i.e. Country Codes, State Codes, etc) within React
问题描述
my reactjs application like most relies a bit on dropdowns that have pretty static data, things such as countries and states. In my application, the values are fetched from the server api.
I am wondering if there was a standard pattern for where to store those values for reuse across an application where that form input may reside on multiply UI's that are not connected.
Would a custom hook be appropriate here, or maybe a singleton service that holds the values that I can populate when the application is initializing?
解决方案
Option 1
You could store data in a javascript file assets/static/data.js
const data = {
states: 'blah',
countries: 'blah',
};
export default data;
Then in your client you can import it
import data from '../your/relative/path/assets/static/data.js';
Option 2
You could also create a .json file and use the json-loader package
Option 3
If you want to have multiple keys to import you can do the following...
export const states = ['Alabama', 'Alaska', ....];
export const countries = ['','', ....];
const data = {
states ,
countries,
};
export default data;
And you can now import the keys like so...
import {states} from '../your/relative/path/assets/static/data.js';
推荐阅读
- here-api - Can't run here-maps sdk Flutter 示例
- swift - 如何在视图(尺寸检查器)xcode 11 中启用宽度和高度?
- reactjs - 在 Azure 应用服务上部署 create-react-app
- ffmpeg - 使用 ffmpeg 的视频反馈循环
- c# - 如何添加更新删除重复的键值对列表
- performance - Pytorch:内存效率加权和,权重沿通道共享
- architecture - 什么是“架构规范”文档?(通常来自英特尔)
- c++ - 如何显示我的模板化向量对?[解决了]
- r - 安装 Rtools 的问题:无法在 PATH 中找到文件
- html - 从 R 中不包含 href 的网站下载 csv(使用 html 按钮)