在哪里/如何存储数据集合

时间:2016-08-13 19:26:09

标签: javascript reactjs

我有一个时区数据集(见下文),我需要访问我应用的不同部分。

我习惯于OO方法,我将它存储在类方法中,例如在Timezone.all_zones(Ruby)中,可以从任何地方访问。

是否有可能做类似的事情,即有一个组件的唯一目的是存储这些数据,然后我可以从我的应用程序的不同部分检索?

否则,存储/检索此静态数据的更多反应方式/位置是什么?

const timezone_objects = [
  {value: "-12.00", text: "(GMT -12:00) Eniwetok, Kwajalein"},
  {value: "-11.00", text: "(GMT -11:00) Midway Island, Samoa"},
  {value: "-10.00", text: "(GMT -10:00) Hawaii"},
  {value: "-9.00", text: "(GMT -9:00) Alaska"},
  {value: "-8.00", text: "(GMT -8:00) Pacific Time (US & Canada)"},
  {value: "-7.00", text: "(GMT -7:00) Mountain Time (US & Canada)"},
  {value: "-6.00", text: "(GMT -6:00) Central Time (US & Canada), Mexico City"},
  {value: "-5.00", text: "(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima"},
  {value: "-4.00", text: "(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz"},
  {value: "-3.50", text: "(GMT -3:30) Newfoundland"},
  {value: "-3.00", text: "(GMT -3:00) Brazil, Buenos Aires, Georgetown"},
  {value: "-2.00", text: "(GMT -2:00) Mid-Atlantic"},
  {value: "-1.00", text: "(GMT -1:00 hour) Azores, Cape Verde Islands"},
  {value: "0.00", text: "(GMT) Western Europe Time, London, Lisbon, Casablanca"},
  {value: "1.00", text: "(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris"},
  {value: "2.00", text: "(GMT +2:00) Kaliningrad, South Africa"},
  {value: "3.00", text: "(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg"},
  {value: "3.50", text: "(GMT +3:30) Tehran"},
  {value: "4.00", text: "(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi"},
  {value: "4.50", text: "(GMT +4:30) Kabul"},
  {value: "5.00", text: "(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent"},
  {value: "5.50", text: "(GMT +5:30) Bombay, Calcutta, Madras, New Delhi"},
  {value: "5.75", text: "(GMT +5:45) Kathmandu"},
  {value: "6.00", text: "(GMT +6:00) Almaty, Dhaka, Colombo"},
  {value: "7.00", text: "(GMT +7:00) Bangkok, Hanoi, Jakarta"},
  {value: "8.00", text: "(GMT +8:00) Beijing, Perth, Singapore, Hong Kong"},
  {value: "9.00", text: "(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk"},
  {value: "9.50", text: "(GMT +9:30) Adelaide, Darwin"},
  {value: "10.00", text: "(GMT +10:00) Eastern Australia, Guam, Vladivostok"},
  {value: "11.00", text: "(GMT +11:00) Magadan, Solomon Islands, New Caledonia"},
  {value: "12.00", text: "(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka"}
]

2 个答案:

答案 0 :(得分:0)

如果这是一个真正的静态资源,不会被修改,你可以将它放在一个自己的js文件中,并在你需要的组件中导入/需要它。

答案 1 :(得分:0)

您可以使用redux

否则对于静态数据,您创建一个data.js文件:

export default function(){
    return[
        {value: "-12.00", text: "(GMT -12:00) Eniwetok, Kwajalein"},
        {value: "-11.00", text: "(GMT -11:00) Midway Island, Samoa"}
    ]
}

然后你可以在这样的应用程序中加载它:

import getData from './data'

然后例如在render方法中:

render(){
    console.log(getData())
    ...