With Vue, I can easily add a Vue application as a wrapper over the server-side rendering HTML like:
<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>
But I can't figure out how to do the same in React. As far as I know, all the content of root div will be replaced, and there is no way to create a kind of wrapper for React app or component. I know, though, I can include separate components but it's not convenient. The ideal would be the same as with Vue - to create a wrapper with functions. Is this possible?
It is super simple, in your index.html
, where you have added the html for the root div, you can add another div
, with id of "component1" or any other name of your choice, and use React.render
to target that div and render a react component (let's call it Component1
as well) onto it. so your modified index.html
file will essentially be:
<!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>
and then in your index.js
file
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();
EDIT
TLDR; You can target any element in the html and render a react component to it using ReactDOM.render()
. You can refer to the docs for usage
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.