【问题标题】:React router server side rendering with Fluxxor使用 Fluxxor 反应路由器服务器端渲染
【发布时间】:2016-02-05 00:13:23
【问题描述】:

我正在尝试将我的 Web 应用实现从 React Router 0.13.x 更新到 1.0.0,它在后端使用 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(或类似的东西)。

React 路由器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')
    }
  })
})

【问题讨论】:

    标签: reactjs flux reactjs-flux


    【解决方案1】:

    state 上的大部分内容现在可通过renderProps 获得。这是您使用的两个 state 属性的映射:

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

    您可以看到通常通过this bit of code 传递的属性

    【讨论】:

    • 谢谢,您的回答实际上帮助我解决了问题。但只是为了记录,state.routes 的等价物是 renderProps.routes 而不是 renderProps.components。
    • 是的,哎呀。我现在已经解决了。谢谢!
    猜你喜欢
    • 2015-09-06
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2017-01-14
    • 2015-04-19
    • 2019-02-09
    • 2018-10-02
    相关资源
    最近更新 更多