简体   繁体   English

Ant Design SSR不匹配问题,服务器呈现中文,但客户端EN

[英]Ant Design SSR mismatch issue, Server renders Chinese but client EN

I added Ant Design to my react/redux SSR ready application. 我将Ant Design添加到了我的React / redux SSR就绪应用程序中。 I used Ant Design's Locale Provider component as they said in documentation. 正如他们在文档中所说的,我使用了Ant Design的Locale Provider组件。 But there is two major problems. 但是有两个主要问题。

  1. In web console, it says; 在Web控制台中说: component rendered Chinese at server but at client it rendered English. 组件在服务器上呈现中文,而在客户端上呈现英文。 So SSR is not compatible. 因此,SSR不兼容。 I tried some web configs but didn't resolve the issue. 我尝试了一些Web配置,但没有解决问题。
  2. It says; 它说; You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size. 您正在使用整个antd软件包,请使用https://www.npmjs.com/package/babel-plugin-import减小应用程序包的大小。 I used babel-plugin-import plugin but didn't change any thing. 我使用了babel-plugin-import插件,但没有做任何更改。

I will be grateful to any suggestion. 我将不胜感激任何建议。

First you must use ant design "LocaleProvider" component as described in documentation. 首先,您必须按照文档中的描述使用蚂蚁设计的“ LocaleProvider”组件。

import { LocaleProvider } from 'antd';
import enUS from 'antd/lib/locale-provider/en_US';

return <LocaleProvider locale={enUS}><App /></LocaleProvider>;

Then to support Server Side Rendering you must use same pattern for server.js file 然后,要支持服务器端渲染,必须对server.js文件使用相同的模式

const { LocaleProvider } = require('antd')
const enUS = require('antd/lib/locale-provider/en_US')

const body = ReactDOMServer.renderToString(
React.createElement(
  LocaleProvider,
  { locale: enUS },
  React.createElement(
    Provider,
    { store },
    React.createElement(
      StaticRouter,
      { location: req.url, context: context },
        React.createElement(Layout, null, React.createElement(Routes))
      )
    )
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM