return React.createElement("div", null, "Hello ", this.props.name);
What does a null
mean in React the above createElement() execution?
createElement() Create and return a new React element of the given type. The type argument can be either a tag name string (such as 'div' or 'span' ), a React component type (a class or a function), or a React fragment type. Code written with JSX will be converted to use React.
Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React. createElement(component, props, ...children) . So, anything you can do with JSX can also be done with just plain JavaScript.
A React Node is one of the following types: Boolean (which is ignored) null or undefined (which is ignored)
To check for null in React, use a comparison to check if the value is equal or is not equal to null , e.g. if (myValue === null) {} or if (myValue !== null) {} . If the condition is met, the if block will run. Copied!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With