反应路由器和漂亮的网址

ser*_*gei 5 single-page-application reactjs react-router react-router-redux

使用react-router漂亮 URL的最佳方法是什么?所有 URL 都存储在 DB 中(总量约为400,000)。当我点击链接时,我需要解析组件以通过AJAX请求从服务器渲染并渲染它。

谢谢!

ser*_*gei 2

我找到了这个问题的答案。您可以使用的方法react-router动态解析组件。例如:getComponentRoute

import ProductPage from '...'
import CategoryPage from '...'

const MAP_PAGE_TYPE_TO_COMPONENT = {
    'productPage': ProductPage,
    'categoryPage': CategoryPage

};

....

const getComponent = (location, callback) => {
  requestToServer(location.pathname)
    .then(function(response){
      const Component = MAP_PAGE_TYPE_TO_COMPONENT[response.pageType];
      callback(null, <Component items = { response.items } />);
     })
    .catch(callback)
};

....

<Route 
  path='*'
  getComponent = { getComponent }
/>
Run Code Online (Sandbox Code Playgroud)

如果没有错误,您应该调用callback函数作为第一个参数,并作为第二个参数。null<Component />