Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed React Components in html pages

Tags:

reactjs

I want build a React component like

class MyComponent extends React.Component {
    render(){
        return (<div>This is a simple component</div>);
    }
}

and use it like

<MyComponent></MyComponent>

in several different pages and even multiple times in a single html page.

I dont want to create a SPA just to enhance my web application's UI with React components.

like image 407
Ioannis Dontas Avatar asked Oct 20 '17 16:10

Ioannis Dontas


People also ask

Can I add a React component to HTML?

Simply adding React components into HTML code is not possible, because <MyComponent></MyComponent> is not HTML at all, it is JSX.

How render HTML component React?

React's goal is in many ways to render HTML in a web page. React renders HTML to the web page by using a function called ReactDOM. render() .


Video Answer


1 Answers

Use

ReactDOM.render(<MyComponent />, document.getElementById('id'));

You can render in your HTML like this:

<div id="id"></div>
like image 89
A R Avatar answered Sep 21 '22 13:09

A R