使用Fluxxor对路由器服务器端进行渲染(React router server side rendering with Fluxxor)

编程入门 行业动态 更新时间:2024-10-14 12:24:27
使用Fluxxor对路由器服务器端进行渲染(React router server side rendering with Fluxxor)

我正在尝试将我的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 Router 服务器渲染示例:

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') } }) })

I'm trying to update my web-app implementation from React Router 0.13.x to 1.0.0, which is using Fluxxor and server side rendering in back-end.

I didn't find any good migration guide to convert this code:

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 }) ); }); }); });

To new version. The below example in the react router is not also clear since it doesn't explain how we can access state.params or state.routes (or something similar) which are necessary to initialize flux stores.

React Router server rendering example:

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') } }) })

最满意答案

现在可以通过renderProps获得大多数state 。 这是您使用的两个state属性的映射:

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

您可以通过这段代码查看通常传递的属性

Most of what's on state is now available via renderProps. Here's a mapping of the two state properties that you use:

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

You can see what properties normally get passed via this bit of code

更多推荐

state,React,routes,Router,params,电脑培训,计算机培训,IT培训"/> <meta name=&q

本文发布于:2023-08-04 03:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   服务器端   React   Fluxxor   side

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!