Intro to React.createelement method with examples

Intro to React.createelement method with examples

In this post we will give you information about Intro to React.createelement method with examples. Hear we will give you detail about Intro to React.createelement method with examplesAnd how to use it also give you demo for it if it is necessary.

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.

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 <span>${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”}.

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

<span>const  el =  React.createElement;function Button(props){  return el('button',{onClick:props.handleClick},props.name);}class Counter extends React<span>.Component{  state= {       num: 0  }  handleIncrement = () =&gt;{    this.setState({      num: this.state.num + 1    })  }   handleDecrement = () =&gt;{    this.setState({      num: this.state.num - 1    })  }  <span>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 &lt;button onClick={props.handleClick}&gt;{props.name}&lt;/button&gt;}class Counter extends React.Component {  state = {    num: 0  }  handleIncrement = () =&gt; {    this.setState({      num: this.state.num + 1    })  }  handleDecrement = () =&gt; {    this.setState({      num: this.state.num - 1    })  }  render() {    return (      &lt;div&gt;        &lt;p&gt;{this.state.num}&lt;/p&gt;        &lt;Button name="Increment" handleClick={this.handleIncrement} /&gt;        &lt;Button name="Decrement" handleClick={this.handleDecrement} /&gt;      &lt;/div&gt;    )  }}ReactDOM.render(&lt;Counter /&gt;, 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.

Hope this code and post will helped you for implement Intro to React.createelement method with examples. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US