我有一套AJAX請求在頁面呈現之前被調用。我正在使用react-router 2.4.1。在我以前的項目,所用反應路由器的舊版本中的一個,這是我用來處理這個問題,那麼如何在狀態轉換之前暫停組件的延遲?
Router.run(RouteConfig, function(Handler, state){
var promises = state.routes.filter(function(route) {
//Every component that needs initial data for rendering will
//define a function fetchData in its statics which will return
//a promise that will be resolved once all required AJAX calls
//are made.
return route.handler.fetchData;
}).map(function(route) {
return route.handler.fetchData(state.params, state.query);
});
if(promises.length > 0) {
Promise.all(promises).then(function(response) {
data = response;
//Rendering will happen only after every call is resolved
render(Handler, data);
}).catch(function(response, err) {
data = response;
data.isError = true;
data.splice(err.index, 1, err.reason);
render(Handler, data);
});
} else {
render(Handler, data);
}
});
function render(Handler, data) {
React.render(<Handler data={data}/>, document.body);
}
在新版本中,沒有Router.run我明白了。我如何在2.4.1中達到同樣的效果?
非常感謝@andre .. –