简体   繁体   English

在为React(react-router)组件安装组件后渲染资产

[英]Assets rendering after component mounts for React (react-router) render

I am using React, React-Router, & Redux in my application. 我在应用程序中使用React,React-Router和Redux。 My issue is that when my route triggers a redirect from my landing page for single sign on to the home component once authenticated, the component renders before the assets (from the root HTML page) are loaded. 我的问题是,一旦我的路线触发了从登陆页面的重定向,以便在通过身份验证后一次登录到home组件,则该组件会在加载资源(来自HTML根页面)之前呈现。 Upon a page refresh the assets are loaded and the CSS renders correctly. 页面刷新后,资产将加载,CSS会正确呈现。 I am confused because the with react this is a SPA and I would have thought the assets would have loaded with the landing page render. 我很困惑,因为with react这是一个SPA,我本以为资产会加载目标网页呈现器。

I have tried the following: 我尝试了以下方法:

  • Adding a setTimeout prior to initiating the API call for authentication in my single sign out landing page 在我的单一退出登录页面中启动用于身份验证的API调用之前添加setTimeout
  • Trying to have a loading indicator render until the component fully mounts (through using the component state to control which content is viewed. 尝试呈现加载指示器,直到组件完全安装为止(通过使用组件状态来控制查看哪些内容。
  • I also tried adding a setTimeout in the toggling of the views in the home component to slow down the switch from the loading view to the main content. 我还尝试在home组件的视图切换中添加setTimeout,以减慢从加载视图到主要内容的切换。

The routing and authentication are working correctly. 路由和身份验证正常工作。 It is only that the assets from the html are loaded after the component appears. 出现组件后,只会加载html中的资产。

I follow the same logic of pushing to the home page via my other two authentication API calls that are initiated through my login pages. 我遵循通过登录页面启动的另外两个身份验证API调用推送到主页的相同逻辑。 This single sign out API call triggers the same method as the other two which ends with a 此单点退出API调用会触发与其他两个方法相同的方法,并以一个结束

history.push('/home')

To explain the process: after the completion of the authentication the action creator pushes to the home page which mounts the component. 解释该过程:完成身份验证后,操作创建者将推送到安装组件的主页。 The component content mounts fine but it renders prior to the assets loading which is a freaking annoying problem. 组件内容可以很好地安装,但是它在资产加载之前进行渲染,这是一个令人生厌的问题。 I'm not sure why this is only happening with the single sign on redirect and not the other two validation processes. 我不确定为什么仅在重定向时使用单点登录而不在其他两个验证过程中才使用。

Again, I tried adding conditional rendering to show a loading image until the component mounts and the state is updated, but this doesn't work as I believe it fails to take into consideration the assets in the html. 再次,我尝试添加条件渲染以显示加载图像,直到组件安装并更新状态为止,但这不起作用,因为我认为它没有考虑html中的资产。

So my question is whether or not there's a better way for me to conditionally render a loading indicator until I can ensure the assets are loaded in the html. 所以我的问题是,是否有更好的方法让我有条件地呈现加载指示符,直到我可以确保将资产加载到html中为止。

UPDATE: I went into chrome dev tools and verified the script tags are loaded. 更新:我进入chrome开发工具并验证了脚本标签已加载。 So basically it isn't processing the css until the refresh... 所以基本上直到刷新后才处理CSS ...

So my issue was with the way the browser was looking for the assets files relative to my urls. 所以我的问题是浏览器查找相对于我的URL的资产文件的方式。 Because I added /ss/:id to the root url the browser was attempting to find the assets with the /ss/:id appended to the base url. 由于我在根网址中添加了/ ss /:id,因此浏览器试图查找将/ ss /:id附加到基本网址后的资产。 This led to them not being found and the page loading without the assets. 这导致找不到它们,并且页面加载时没有资产。 With the page refresh the url was back to the root url and the assets were able to be identified. 页面刷新后,URL返回到根URL,并且可以识别资产。

I decided to fix this by utilizing the 'this.props.location.search' option with react-router along with the npm package 'qs' which allowed me to parse the string and select the id from the url. 我决定通过使用带有react-router的'this.props.location.search'选项和npm包'qs'来解决此问题,这使我能够解析字符串并从URL中选择ID。

so my react route in the index.js page looks like: 所以我在index.js页面中的反应路线如下所示:

<Route path="/ss" component={SSAuth} />

and in my SSAuth file (single sign on): 并在我的SSAuth文件中(单点登录):

 componentDidMount(match) {
 const value = qs.parse(this.props.location.search, {ignoreQueryPrefix : true})
 console.log(value)
 this.props.ssauthentication(value.id, this.props.history)


}

I had to add the optional parameter {ignoreQueryPrefix : true} so the search query '?' 我必须添加可选参数{ignoreQueryPrefix:true},以便搜索查询'?' would be ignored. 将被忽略。

so with the redirect url looking something like 因此,重定向网址看起来像

https://baseurl.com/ss?id=example

my code with identify and parse out 'example' and then pass that to my action method 'this.props.ssauthentication' which triggers the authentication api call in my actions_index.js file. 我的代码带有识别并解析出“示例”,然后将其传递给我的操作方法“ this.props.ssauthentication”,该方法将触发我的actions_index.js文件中的身份验证api调用。

*note: I pass this.props.history from react-router as well to allow for the page redirect and home component to mount once the api calls have finished. *注:我也从react-router传递了this.props.history,以允许页面重定向和home组件在api调用完成后挂载。

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

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