简体   繁体   English

如何在服务器端渲染单个组件?

[英]How to server side render individual components in react?

I have a react application with spring backend.我有一个带有 spring 后端的反应应用程序。 I want to server side render a specific component.我想在服务器端渲染一个特定的组件。
My react application runs on port 3000 and spring application on port 8080. How can I just put one component on spring rather than an entire route?我的 react 应用程序在端口 3000 上运行,spring 应用程序在端口 8080 上运行。我怎样才能将一个组件放在 spring 而不是整个路由上?

You haven't shared a lot about your setup regarding frontend nor how it does it communicate with the backend service so it's hard to give a precise answer.您没有分享很多关于前端的设置以及它如何与后端服务通信的信息,因此很难给出准确的答案。

First of all, ensure you already have server side rendering (SSR) engine up and running.首先,确保您已经启动并运行了服务器端渲染 (SSR) 引擎。 This will not be the case if you are using something like create-react-app which works only client-side.如果您使用像 create-react-app 这样只在客户端工作的东西,情况就不是这样了。

If you have SSR setup, please have a closer look at ReactDOM.renderToString/renderToNodeStream/renderToStaticMarkup (depending on which one is used).如果您有 SSR 设置,请仔细查看 ReactDOM.renderToString/renderToNodeStream/renderToStaticMarkup(取决于使用哪一个)。 There you can add your logic how to decide which part of component's tree is rendered.在那里你可以添加你的逻辑如何决定组件树的哪一部分被渲染。

You can use ThymeLeaf / Moustache for SSR but if you're specific only to React SSR then you'll need an Express.JS based server returning SSR HTML in response to the request made by the client.您可以将 ThymeLeaf / Mustache 用于 SSR,但如果您只针对 React SSR,那么您需要基于 Express.JS 的服务器返回 SSR HTML 以响应客户端的请求。

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

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