简体   繁体   English

React / Redux应用程序(例如Pace.js)的加载百分比栏,仅在初始页面加载时显示

[英]Percentage loading bar for React / Redux app like Pace.js that only shows on initial page load

I'm trying to find the best way to have a loading bar for my website done in React (create-react-app) / Redux that shows the percentage of the page load. 我正在尝试找到在React(create-react-app)/ Redux中完成网站加载栏的最佳方法,以显示页面加载百分比。

However I only want this to appear once during the initial page load and every subsequent ajax call or page transition there shouldn't be a loading bar. 但是我只希望在初始页面加载期间出现一次,并且随后的每次ajax调用或页面转换都不应有加载栏。 I'm using Pace.js currently and the way I do it with that is to just include the Pace scripts / stylesheets outside of the build. 我目前正在使用Pace.js ,而这样做的方法是仅在构建之外包括Pace脚本/样式表。 Then I do a inline script in the index.html 然后我在index.html中做一个内联脚本

Pace.once('done', () => remove specific pace stylesheet

However, the problem is that since this code exists outside of the React bundle code, I can't utilize Redux and state management. 但是,问题在于,由于该代码存在于React bundle代码之外,因此我无法利用Redux和状态管理。 I want to basically have an initial state key initialPageLoad set to true in Redux and so depending on which route the user starts on, I will have different transitions from the loader bar. 我基本上希望在Redux中将初始状态键initialPageLoad设置为true ,因此根据用户开始使用的路线,我将从加载器栏获得不同的过渡。 After this transition, I will set initialPageLoad to false so the loader doesn't run on subsequent navigations. 过渡之后,我将initialPageLoad设置为false,以便加载程序不会在后续导航中运行。

One solution I can think of is to import Pace into React and then I would have access to Redux. 我能想到的一种解决方案是将Pace导入React,然后可以访问Redux。 However, I think it would be tricky to include Pace into React's lifecycle hooks; 但是,我认为将Pace包含在React的生命周期挂钩中是很棘手的。 in the Create React App repo, they even have a side note for Pace here , recommending to include it as a separate CDN. 在Create React App仓库中,他们甚至在此处有Pace的旁注,建议将其作为单独的CDN包含在内。

I couldn't find any Pace-like libraries for React out there; 我在那里找不到React的任何类似Pace的库; the only progress loaders are just components that you need to modify themselves so any advice would be appreciated. 唯一的进度加载程序只是您需要自行修改的组件,因此,任何建议都将不胜感激。

Trying to integrate Pace into your Redux state management is impractical, as trying to get the app to report on whether it has loaded its assets is a concern separate from an instance of the app. 尝试将Pace集成到Redux状态管理中是不切实际的,因为试图使应用程序报告其是否已加载资产是与应用程序实例分开的关注点。

One approach you could take is to store your applications initial page load flag in HTML5 session storage . 您可以采用的一种方法是将应用程序的初始页面加载标记存储HTML5会话存储中

Then when your page loads for the first time you could update a session variable to true eg 然后,当您的页面首次加载时,您可以将会话变量更新为true,例如

Pace.once('done', () => { sessionStorage.setItem('isInitialPageLoaded', true); })

On subsequent page loads you could then check this variable to see if this page has had its assets loaded previously: 在随后的页面加载中,您可以检查此变量以查看此页面之前是否已加载其资产:

 const isInitialPageLoaded = 'isInitialPageLoaded'
 Pace.once('restart', () => { if (sessionStorage.getItem(isInitialPageLoaded)) {
  // Trigger Pace to display page load progress
  sessionStorage.setItem(isInitialPageLoaded, true);
 });

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

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