简体   繁体   English

Javascript:为单个页面应用程序存储全局变量

[英]Javascript: Store a global variable for a single page application

A have a react application using a single page. 一个使用单个页面的React应用程序。 I call index.html and use ajax(axio) and update the page. 我调用index.html并使用ajax(axio)并更新页面。 I do not use any routing. 我不使用任何路由。

I have some global variables and will use in the whole scope of the application. 我有一些全局变量,将在整个应用程序范围内使用。 The variables are primitive type integer for example. 例如,变量是原始类型整数。 Some of the variables may be updated during the app lifecycle and some remain constant. 有些变量可能会在应用程序生命周期中进行更新,而某些则保持不变。 But I should be able to reach them from all react components/javascripts. 但是我应该能够从所有react组件/ javascripts中找到它们。 They may contain business related constants or example decimal format mask, keeping them private in each component will not be useful. 它们可能包含与业务相关的常量或示例十进制格式的掩码,将它们在每个组件中保持私有将无用。

There is no need/reason to store it on the disk (localStorage). 无需/无需将其存储在磁盘(localStorage)上。

Question is: what is the best way (performance etc) storing the global variable foo in the app? 问题是:在应用程序中存储全局变量foo的最佳方法(性能等)是什么?

window.foo = 10 or
window.sessionStorage.foo = 10

AFAIK, window.foo is for a single page and sessionStorage allows using within multiple pages within the same origin. AFAIK,window.foo用于单个页面,sessionStorage允许在同一来源的多个页面中使用。 What is best in my case when I use a single page? 在我使用单个页面的情况下,最好的方法是什么? Are there any other drawbacks of window.foo usage? 使用window.foo是否还有其他缺点? Security is not important, the vales stored are not sensitive. 安全性并不重要,存储的值不敏感。 Most critical is the performance. 最关键的是性能。

You probably want to use context rather than either of those. 您可能要使用上下文,而不要使用其中任何一个。 From that documentation: 从该文档中:

Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language. 上下文旨在共享可被视为React组件树(例如当前经过身份验证的用户,主题或首选语言)的“全局”数据。

Definitely don't use global variables. 绝对不要使用全局变量。 The global namespace is incredibly crowded, and adding to it is generally not best practice. 全局名称空间非常拥挤,将其添加通常不是最佳实践。 If you did use a global for this, I'd recommend using just one, and having it refer to an object with properties for the various pieces of information you want to share. 如果确实为此使用了全局变量,则建议仅使用一个全局变量,并使其引用一个对象,该对象具有要共享的各种信息的属性。 Note that they'll be window-specific. 请注意,它们将是特定于窗口的。

One reason to possibly consider using sessionStorage (or perhaps periodically synchronizing your React context to it) is if you want changes in one window to be reflected in another window. 可能考虑使用sessionStorage原因(或者可能定期将其React上下文与其同步)是因为您希望一个窗口中的更改反映在另一个窗口中。 Two windows/tabs from the same origin share the same sessionStorage and can get an event ( storage ) when the other one changes that storage. 来自同一源的两个窗口/选项卡共享相同的sessionStorage并且当另一个窗口/选项卡更改该存储时可以获取事件( storage )。 So if the global information were an app theme (say, light vs. dark), changing it in one tab could also affect the other tab if you respond to the storage event by updating your React context. 因此,如果全局信息是应用程序主题(例如,浅色与深色),则如果您通过更新React上下文来响应storage事件,则在一个选项卡中对其进行更改也会影响另一个选项卡。 Note that sessionStorage (and localStorage ) only store strings , so the usual thing is to convert to JSON when storing ( JSON.stringify ) and from JSON when loading ( JSON.parse ). 请注意, sessionStorage (和localStorage )仅存储字符串 ,因此通常的做法是在存储( JSON.stringify )时转换为JSON,而在加载( JSON.parse )时转换为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM