thinker
thinker

Reputation: 422

What does a null mean in React createElement()?

return React.createElement("div", null, "Hello ", this.props.name);

What does a null mean in React the above createElement() execution?

Upvotes: 6

Views: 11770

Answers (3)

user6184932
user6184932

Reputation:

The null is React.createElement is reserved for giving attribute values to the element. If you have none to pass then you put in a null, else you pass the attribute in a javascript object form. See example below. Here className is used for class as that is what is used in jsx to define html class.

import React, { Component } from 'react';
import './App.css';


class App extends Component {
  render() {
    return React.createElement('div', { className: "App" }, React.createElement('h1', null,'Title'));
  }
}

export default App;

This example outputs <div class="App"><h1>Title</h1></div>

Upvotes: 2

Praveen Prasad
Praveen Prasad

Reputation: 32137

React.createElement(type, props, children);

The props parameter is a JavaScript object passed from a parent element to a child element. When you say null means you are not passing any props to that component.

~~

type in React.createElement can be a string like h1, div etc or a React-Component.

var reactElement = React.createElement('div', { className: 'header' });

In this particular case when you pass prop { className: 'header' } , createddiv element will have cssClass associated with it.

Upvotes: 11

Jorawar Singh
Jorawar Singh

Reputation: 7621

It's just a null.

It means that the function waiting for some parameters. The second parameter is null so function know that it's null coming in if you don't pass null as second then your third parameter will be second and function will treat your third argument as second and things will goes wrong. There is for sure you can pass something else then null but I don't know that. Read here

Upvotes: 1

Related Questions