2015-11-04 154 views
0

我試圖更新我的Web應用程序實現從React路由器0.13.x到1.0.0,這是使用Fluxxor和服務器端渲染在後端。React路由器服務器端與Fluxxor渲染

我沒有找到什麼好的遷移指南,把這段代碼轉換:

something.serve(function (req, res) { 
    Router.run(routes, req.path, function (Root, state) { 

     fetchData(state.routes).then(function (data) { 
      var flux = new Flux(null, user, data, state.params, currencies, categories, sortTypes), 
       serializedFlux = flux.serialize(), 
       content = React.renderToString(
        React.createElement(Handler, { 
         flux: flux, 
         params: state.params 
        }) 
       ); 
     }); 
    }); 
}); 

到新版本。反應路由器中的下面的例子並不清楚,因爲它沒有解釋我們如何訪問state.params或state.routes(或類似的東西),這些都是初始化flux存儲所必需的。

陣營路由器server rendering例如:現在

import { renderToString } from 'react-dom/server' 
import { match, RoutingContext } from 'react-router' 
import routes from './routes' 

serve((req, res) => { 
    // Note that req.url here should be the full URL path from 
    // the original request, including the query string. 
    match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { 
    if (error) { 
     res.status(500).send(error.message) 
    } else if (redirectLocation) { 
     res.redirect(302, redirectLocation.pathname + redirectLocation.search) 
    } else if (renderProps) { 
     res.status(200).send(renderToString(<RoutingContext {...renderProps} />)) 
    } else { 
     res.status(404).send('Not found') 
    } 
    }) 
}) 

回答

0

大部分什麼在state是通過renderProps可用。下面是使用兩個state性質的映射:

  • state.routes - >renderProps.routes
  • state.params - >renderProps.params

你可以看到性能通常會得到通過this bit of code

+0

感謝過去了,你的回答實際上幫助我解決了這個問題。但只是爲了記錄相當於state.routes是renderProps.routes而不renderProps.components。 – p0o

+0

是的,哎呀。我現在修復了這個問題。謝謝! –