you can simply insert all off your html code in App.js file, reactjs doesn't require any .html file. just follow below code, hope you can understand it. i simply put all HTML code into parent div of App.js.
import React from 'react';
var perf =require('./App.html');
class App extends React.Component {
render(){
return (
<div>
<div class="carousel">
<div class="carousel-item">
<img src={require("./1.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./2.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./3.png")} class="responsive-img"></img>
</div>
</div>
</div>
);
}
}
export default App;
Answer from Fazal on Stack Overflowyou can simply insert all off your html code in App.js file, reactjs doesn't require any .html file. just follow below code, hope you can understand it. i simply put all HTML code into parent div of App.js.
import React from 'react';
var perf =require('./App.html');
class App extends React.Component {
render(){
return (
<div>
<div class="carousel">
<div class="carousel-item">
<img src={require("./1.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./2.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./3.png")} class="responsive-img"></img>
</div>
</div>
</div>
);
}
}
export default App;
Just use react-create-app template and the basic html (head, meta) things are already in place, Just modify the src dir to meet you needs.
for example the index.js would be something like this,
import React from 'react';
import './App.css'; // For Your CSS file
class App extends React.Component {
render(){
return (
<div class="carousel">
<div class="carousel-item">
<img src={require("./1.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./2.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./3.png")} class="responsive-img"></img>
</div>
);
}
}
export default App;
if your need to change the base html you can do so in the public dir.
EDIT: (For the dependencies)
As you use materialize-css, You can use that by installing materialize-css@next as a dependency using npm with this cmd npm install materialize-css@next
More info about installing can be found here
Source: https://materializecss.com/getting-started.html
you can simply insert all off your html code in App.js file, reactjs doesn't require any .html file. just follow below code, hope you can understand it. i simply put all HTML code into parent div of App.js.
import React from 'react';
var perf =require('./App.html');
class App extends React.Component {
render(){
return (
<div>
<div class="carousel">
<div class="carousel-item">
<img src={require("./1.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./2.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./3.png")} class="responsive-img"></img>
</div>
</div>
</div>
);
}
}
export default App;
Just use react-create-app template and the basic html (head, meta) things are already in place, Just modify the src dir to meet you needs.
for example the index.js would be something like this,
import React from 'react';
import './App.css'; // For Your CSS file
class App extends React.Component {
render(){
return (
<div class="carousel">
<div class="carousel-item">
<img src={require("./1.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./2.png")} class="responsive-img"></img>
</div>
<div class="carousel-item">
<img src={require("./3.png")} class="responsive-img"></img>
</div>
);
}
}
export default App;
if your need to change the base html you can do so in the public dir.
EDIT: (For the dependencies)
As you use materialize-css, You can use that by installing materialize-css@next as a dependency using npm with this cmd npm install materialize-css@next
More info about installing can be found here
Source: https://materializecss.com/getting-started.html
Insert HTML with React Variable Statements (JSX) [duplicate]
reactjs - How to embed React Components in html pages - Stack Overflow
How to embed HTML in react components?
Using React with plain HTML
Videos
If you're starting out, I recommend you bootstrap your apps using npx create-react-app. It'll give you a good sense of what a React app could look like, and some pointers for file structure.
Most React apps have an index.html file, which you can use like any normal HTML file. But, for the majority of your app, it's recommended to write your content in JSX (otherwise, you aren't getting the benefits of using React in the first place).
JSX
JSX looks very similar to regular HTML, with a handful of key differences:
- Tag attributes tend to be in lowerCamelCase (
onChangerather thanonchange) - Instead of
class(which is a reserved keyword in JavaScript), you need to useclassName
An Example Component
I've borrowed this sample code from React's official tutorial, which you should definitely check out if you haven't already.
This is a class Component, and your JSX goes inside of the render method:
import React from 'react';
class ShoppingList extends React.Component {
render() {
return (
<div className="shopping-list">
<h1>Shopping List for {this.props.name}</h1>
<ul>
<li>Instagram</li>
<li>WhatsApp</li>
<li>Oculus</li>
</ul>
</div>
);
}
}
What goes in index.html?
The only essential part of index.html is a <div id="root"></div>, which React will use to append the rest of the JSX.
This is also the place to add the usual metadata and icons.
As an example, here's the index.html file that comes with create-react-app. For most of my projects, I leave this pretty-much as-is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut 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="logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
In any given React component, there can only be one parent/top layer html element. You can get around this by using <React.Fragment> ...the rest of your html ... </React.Fragment> (or <>...</> depending on your version) or simply add a wrapping <div> around everything. JSX doesn't distinguish between "normal" html and "React" html, it just turns the React stuff into normal html (over simplification, but close enough for this question). Try it again and let me know if you encounter any problems.
const reactElement = (
<div>
React stuff
</div>
);
ReactDOM.render(
reactElement,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div>
<div id="root">
</div>
<div>
just normal html
</div>
</div>
You can use dangerouslySetInnerHTML, e.g.
render: function() {
return (
<div className="content" dangerouslySetInnerHTML={{__html: thisIsMyCopy}}></div>
);
}
Note that dangerouslySetInnerHTML can be dangerous if you do not know what is in the HTML string you are injecting. This is because malicious client side code can be injected via script tags.
It is probably a good idea to sanitize the HTML string via a utility such as DOMPurify if you are not 100% sure the HTML you are rendering is XSS (cross-site scripting) safe.
Example:
import DOMPurify from 'dompurify'
const thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong></p>';
render: function() {
return (
<div className="content" dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(thisIsMyCopy)}}></div>
);
}
Simply adding React components into HTML code is not possible, because <MyComponent></MyComponent> is not HTML at all, it is JSX.
Explaination
JSX is a special syntax that can be 'transpiled' to Javascript, so in essence <MyComponent></MyComponent> will end up beeing Javascript code, which obviously can not just be put into HTML code.
The Javascript code generated from JSX then will be executed and generates actual HTML code.
It is possible to add HTML tags into JSX, because HTML can be interpreted as JSX (and will be transpiled to Javascript as well), like:
class MyComponent extends React.Component {
render(){
return <div>
<h2>HTML in JSX works</h2>
<SomeOtherJsxComponent />
</div>;
}
}
But it is not possible to add JSX into HTML, like:
<body>
<div>
<JsxInHtmlDoesNotWork />
</div>
</body>
React is Javascript, so everything that is necessary to add Javascript functionality to HTML also applies to adding React to HTML.
(nearest) Solution
So what you could do is to move your existing HTML into to some JSX wrapper (which is probably not what you would like to do, because this goes in the direction of creating a SPA, what you don't want), e.g.:
<html><head>
<title>My web site</title>
</head><body>
<h1>Some HTML title</h1>
<p>Some HTML content.</p>
<!-- add a container, where you want to include react components -->
<div id="injected-react-content"></div>
<!-- import the react libraray -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- setup react root component other components -->
<script type="text/babel">
class RootComponent extends React.Component {
render(){
return <div>
<MyComponent />
</div>;
}
}
class MyComponent extends React.Component {
render(){
return (<div>This is a simple component</div>);
}
}
const domContainer = document.querySelector('#injected-react-content');
ReactDOM.render( React.createElement(RootComponent), domContainer );
</script>
</body></html>
For some more background information on how to add React to an existing HTML website, see e.g.:
- stackoverflow.com/questions/65917670/how-to-use-react-components-as-part-of-legacy-html-js-app
- stackoverflow.com/questions/69607103/react-component-not-displayed-in-html
Use
ReactDOM.render(<MyComponent />, document.getElementById('id'));
You can render in your HTML like this:
<div id="id"></div>
What's the best way to embed Html provided by other sites into a react component? I'm looking for some examples.
ยป npm install html-to-react
You can use dangerouslySetInnerHTML, e.g.
render: function() {
return (
<div className="content" dangerouslySetInnerHTML={{__html: thisIsMyCopy}}></div>
);
}
Note that dangerouslySetInnerHTML can be dangerous if you do not know what is in the HTML string you are injecting. This is because malicious client side code can be injected via script tags.
It is probably a good idea to sanitize the HTML string via a utility such as DOMPurify if you are not 100% sure the HTML you are rendering is XSS (cross-site scripting) safe.
Example:
import DOMPurify from 'dompurify'
const thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong></p>';
render: function() {
return (
<div className="content" dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(thisIsMyCopy)}}></div>
);
}