by Sai gowtham

Intro to React.createelement method with examples

In this tutorial, we are going to learn how to write react without jsx.

The JSX we write inside the react is often transpiled into a React.createElement() method with the help of babel compiler.

React.createElement() method takes the three arguments type , props ,children.

type: Type of the html element or component (example : h1,h2,p,button, etc).

props: The properties object (example: {style: { color: “red” }} or className or event handlers etc).

children: anything you need to pass between the dom elements.

Simple example

let welcome = React.createElement(
  "h1",
  { style: { color: "red" } },
  `Welcome to react world`
);

ReactDOM.render(welcome, document.querySelector("#root"));

ReactDom.render method accepts two arguments.

  • The first argument is which component or element needs to render in the dom.

  • The second argument is where to render in the dom.

react createelement example

This is a pure JavaScript without using jsx.

Let’s refactor the above code by creating a Welcome component with the classes.

class Welcome extends React.Component {
  render() {
    return React.createElement(
      "h1",
      { style: { color: "red" } },
      `Welcome to ${this.props.name}`
    );
  }
}

ReactDOM.render(
  React.createElement(Welcome, { name: "Home page" }, null),  document.querySelector("#root")
);

Inside the Welcome Component, we have added the props. so that we have passed the props data{name: "Home page"}.

react without jsx

So far we are building components with no user interaction let’s build an interactive component with the state and event Handling.

Counter component example

const  el =  React.createElement;

function Button(props){
  return el('button',{onClick:props.handleClick},props.name);
}

class Counter extends React.Component{

  state= {
       num: 0
  }

  handleIncrement = () =>{
    this.setState({
      num: this.state.num + 1
    })
  }

   handleDecrement = () =>{
    this.setState({
      num: this.state.num - 1
    })
  }

  render(){    return el('div',null,                  el(Button,{handleClick:this.handleIncrement,name:'Increment'},null),                  el(Button,{handleClick:this.handleDecrement,name:'Decrement'},null),                  el('p',null,this.state.num)    )
  }
}

ReactDOM.render(el(Counter,null,null),document.querySelector('#root'));

Code pen demo

Let’s see the same Counter example by using JSX.

function Button(props) {
  return <button onClick={props.handleClick}>{props.name}</button>
}

class Counter extends React.Component {

  state = {
    num: 0
  }

  handleIncrement = () => {
    this.setState({
      num: this.state.num + 1
    })
  }

  handleDecrement = () => {
    this.setState({
      num: this.state.num - 1
    })
  }

  render() {
    return (
      <div>
        <p>{this.state.num}</p>
        <Button name="Increment" handleClick={this.handleIncrement} />
        <Button name="Decrement" handleClick={this.handleDecrement} />
      </div>

    )
  }
}


ReactDOM.render(<Counter />, document.querySelector('#root'))

By using JSX, the code readability increases and also we can create components easily this is the reason we use jsx in react apps.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2022 (Beginner + Advanced)
JavaScript - The Complete Guide 2022 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY