【问题标题】:React: how to mix server-side HTML with react components and functionsReact:如何将服务器端 HTML 与 React 组件和函数混合使用
【发布时间】:2020-12-18 10:34:50
【问题描述】:

使用 Vue,我可以轻松地将 Vue 应用程序添加为服务器端呈现 HTML 的包装器,例如:

<body>
  <div id="appMain">
    <!-- This is the root element for Vue -->

    <!-- This is a server-side code -->

    <main role="main" class="container">
      <div class="row">
        <div class="col-md-8 items-list">
          <div
            class="item row no-gutters rounded overflow-hidden flex-md-row mb-4 h-md-250 position-relative"
          >
            <div
              class="col-md-3 col-sm-auto d-lg-block image-block"
              style="background-image: url(${item.cover_image_url}); background-color: ${item.cover_image_back or 'transparent'}"
            >
              <!--img src="${item.cover_image_url}" class="col-12"-->
            </div>
          </div>
        </div>
        <div>
          <!-- This is code for subscription, done in Vue -->
          <div class="subscription-form">
            <input
              type="email"
              placeholder="Enter your email"
              v-model="email"
            />
            <button class="" @click="subscribe()">
              <i class="far fa-paper-plane"></i>
            </button>
          </div>
          <div
            :class="{'message-success': success, 'message-error': error, 'message-info': info}"
          >
            {{message}}
          </div>
        </div>
      </div>
    </main>
  </div>
</body>

但我不知道如何在 React 中做同样的事情。据我所知,根 div 的所有内容都会被替换,并且没有办法为 React 应用程序或组件创建一种包装器。不过,我知道,我可以包含单独的组件,但这并不方便。理想情况与 Vue 相同——创建一个带有函数的包装器。这可能吗?

【问题讨论】:

  • 也可以使用 react 来渲染 HTML 服务端
  • @AlexeiLevenkov 谢谢,但我使用 Python,所以几乎不可能。

标签: javascript reactjs vue.js


【解决方案1】:

非常简单,在您的index.html 中,您已经添加了根 div 的 html,您可以添加另一个 div,id 为“component1”或您选择的任何其他名称,然后使用 @ 987654324@ 以该 div 为目标并在其上渲染一个反应组件(我们也称它为Component1)。所以你修改后的index.html 文件基本上是:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <div id="component1"></div> <!-- This is your defined html div -->
  </body>
</html>

然后在您的index.js 文件中

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

//add the below lines for a component called Component1

import Component1 from "./Component1.js";

ReactDOM.render(
    <Component1 />,
  document.getElementById('component1')
);

serviceWorker.unregister();

编辑

TLDR; 您可以定位 html 中的任何元素,并使用 ReactDOM.render() 向其呈现反应组件。使用方法可以参考docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2015-10-08
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多