【发布时间】:2016-04-25 11:58:57
【问题描述】:
我想知道在同构React 应用程序中输出基于system(服务器端)的日期/时间的良好做法是什么。确实有很多方法(ajax,环境验证等),但问题是关于真正的实践。
例如:目标是在商标的页脚中输出当前年份。
仅供参考:我正在使用react-router 模块,所以渲染看起来像这样:
ReactDOMServer.renderToString(<RoutingContext {...renderProps} />)
更新:
如果我尝试使用 Route 这样的道具:
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
//...
if (renderProps) {
renderProps.params.year = (new Date).getFullYear();
res.status(200).send(ReactDOMServer.renderToString(<RoutingContext {...renderProps} />));
}
//...
});
在 jsx 内部:
<Footer year={ this.props.params.year } />
但出现以下错误:
未捕获的错误:不变违规:您正在尝试渲染 使用服务器呈现的文档组件,但校验和是 无效的。这通常意味着您渲染了不同的组件类型或 客户端上的道具来自服务器上的道具,或者您的 render() 方法不纯。
我们将非常感谢您的帮助!
【问题讨论】:
标签: javascript datetime reactjs react-router isomorphic-javascript