How do you render html content in 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[].

The Render Function

The ReactDOM.render[] function takes two arguments, HTML code and an HTML element.

The purpose of the function is to display the specified HTML code inside the specified HTML element.

But render where?

There is another folder in the root directory of your React project, named "public". In this folder, there is an index.html file.

You'll notice a single

in the body of this file. This is where our React application will be rendered.

Example

Display a paragraph inside an element with the id of "root":

ReactDOM.render[

Hello

, document.getElementById['root']];

The result is displayed in the

element:


  

Run Example »

Note that the element id does not have to be called "root", but this is the standard convention.

The HTML Code

The HTML code in this tutorial uses JSX which allows you to write HTML tags inside the JavaScript code:

Do not worry if the syntax is unfamiliar, you will learn more about JSX in the next chapter.

Example

Create a variable that contains HTML code and display it in the "root" node:

const myelement = [
  
      Name
    
John
Elsa
]; ReactDOM.render[myelement, document.getElementById['root']];

Run Example »

The Root Node

The root node is the HTML element where you want to display the result.

It is like a container for content managed by React.

It does NOT have to be a

element and it does NOT have to have the id='root':

Example

The root node can be called whatever you like:



  


Display the result in the element:

ReactDOM.render[

Hallo

, document.getElementById['sandy']];

Run Example »


So is this the only way to render raw html with reactjs?

// //facebook.github.io/react/docs/tutorial.html
// tutorial7.js
var converter = new Showdown.converter[];
var Comment = React.createClass[{
  render: function[] {
    var rawMarkup = converter.makeHtml[this.props.children.toString[]];
    return [
      

{this.props.author}

]; } }];

I know there are some cool ways to markup stuff with JSX, but I am mainly interested in being able to render raw html [with all the classes, inline styles, etc..]. Something complicated like this:


Dropdown
  • Action
  • Another action
  • Something else here
  • Separated link

I would not want to have to rewrite all of that in JSX.

Maybe I am thinking about this all wrong. Please correct me.

Action

  • Another action
  • Something else here
  • Separated link
  • ` const YourComponent = [] => [
    {
    }
    ] export default YourComponent

    answered May 31, 2019 at 11:22

    YuciYuci

    24.5k9 gold badges102 silver badges109 bronze badges

    1

    You could leverage the html-to-react npm module.

    Note: I'm the author of the module and just published it a few hours ago. Please feel free to report any bugs or usability issues.

    answered Jun 21, 2015 at 0:02

    Mike NiklesMike Nikles

    1,39010 silver badges16 bronze badges

    8

    I have used this in quick and dirty situations:

    // react render method:
    
    render[] {
        return [
          
    { this.props.textOrHtml.indexOf['

    Chủ Đề