admin管理员组文章数量:1310498
We are building our website with react/react-router/redux
We want to server side render our pages that should be filled by the data from our data sources. This transaction has to be asynchronous and unfortunately since we want to server side render, we can not use "ponentDidMount" function.
In the redux tutorial page at server side rendering section here, it has been advised to :
If you use something like React Router, you might also want to express your data fetching dependencies as static fetchData() methods on your route handler ponents. They may return async actions, so that your handleRender function can match the route to the route handler ponent classes, dispatch fetchData() result for each of them, and render only after the Promises have resolved. This way the specific API calls required for different routes are colocated with the route handler ponent definitions. You can also use the same technique on the client side to prevent the router from switching the page until its data has been loaded.
This is currently how we handle our data fetch. I personally did not like this approach it looks quite clumsy and it is too coupled to the routing library. Are there any better ways to do it - hopefully with standard react/router/redux ponents ?
We are building our website with react/react-router/redux
We want to server side render our pages that should be filled by the data from our data sources. This transaction has to be asynchronous and unfortunately since we want to server side render, we can not use "ponentDidMount" function.
In the redux tutorial page at server side rendering section here, it has been advised to :
If you use something like React Router, you might also want to express your data fetching dependencies as static fetchData() methods on your route handler ponents. They may return async actions, so that your handleRender function can match the route to the route handler ponent classes, dispatch fetchData() result for each of them, and render only after the Promises have resolved. This way the specific API calls required for different routes are colocated with the route handler ponent definitions. You can also use the same technique on the client side to prevent the router from switching the page until its data has been loaded.
This is currently how we handle our data fetch. I personally did not like this approach it looks quite clumsy and it is too coupled to the routing library. Are there any better ways to do it - hopefully with standard react/router/redux ponents ?
Share Improve this question edited Jul 5, 2016 at 10:18 ralzaul asked Mar 17, 2016 at 15:50 ralzaulralzaul 4,4706 gold badges36 silver badges52 bronze badges 2- "This transaction has to be asynchronous" Why? – flup Commented Mar 17, 2016 at 18:35
- Very often data fetching from APIs is async so that you don't block the main thread, especially if you are retrieving from multiple APIs you do not want one to wait for the other if possible. – Kevin Farrugia Commented Oct 18, 2017 at 18:37
1 Answer
Reset to default 8Something like a static fetchData()
method is the correct way to handle data fetching with React Router in the general case, though it can reach down into child ponents as needed (which is e.g. how Relay works).
The reason you want to do it this way is that React Router resolves all the matched routes all at once. Given that, you can then run data fetching for all of your route handlers simultaneously.
If instead you tied data fetching to instance-level handlers on ponents, you'd always end up with fetch waterfalls, where a ponent could not fetch its required data until all of its parents receive their required data, and so forth. While that may not be a big problem on the server, it's hugely suboptimal on the client.
If you really want to colocate data dependencies to ponents, you can consider using something like React Resolver, but this can easily lead to a suboptimal experience for your users.
本文标签: javascriptServer side rendering with async data fetchStack Overflow
版权声明:本文标题:javascript - Server side rendering with async data fetch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819943a2399303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论