Use React Variables inside HTML

Using react js code inside html code. Print HTML data dynamically from react js variables.

Printing name variable inside HTML code using:

{name}

React JS code

import React from "react";
import ReactDOM from "react-dom";

const name = "SmartCoder";
ReactDOM.render(
  <div>
    <h1> My name is {name}</h1>
    <p> My blog url is smartcodehelper.com. </p>
    <p> My Lucky number is {Math.floor(Math.random()*10)} </p>
  </div>
  ,document.getElementById("root")
);

Output:

Example 2:

import React from "react";
import ReactDOM from"react-dom";

const name= "Smart Coder";
var date = new Date();
var currentYear = date.getFullYear();

ReactDOM.render(
  <div>
    <p> Created by {name} </p>
    <p> Copyright {currentYear}</p>
  </div>
  , document.getElementById("root"));

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *