phuocng / this-vs-that

Understanding the difference between ˍˍˍ and ˍˍˍ in front-end development
https://phuoc.ng/collection/this-vs-that
MIT License
1.47k stars 98 forks source link

[React] React.createElement() vs JSX #282

Closed phuocng closed 1 year ago

MkDev11 commented 1 year ago

'React.createElement()' and JSX are two ways of defining and creating React elements in a React application. They serve the same purpose but offer different syntax and readability. Here's a comparison of the two approaches:

1. 'React.createElement()':

'React.createElement()' is the foundational method provided by React to create React elements. It takes three arguments: the type of element (such as a component or HTML tag), an optional object of properties (props), and the element's children.

Example using 'React.createElement()':

const element = React.createElement('div', { className: 'container' }, 'Hello, World!');

In this example, 'element' represents a React element, which can be rendered to the DOM using 'ReactDOM.render()'.

2. JSX:

JSX is a syntax extension for JavaScript that allows you to write HTML-like code directly within your JavaScript files. JSX is transformed into 'React.createElement()' calls by build tools like Babel before it's executed in the browser.

Example using JSX:

const element = <div className="container">Hello, World!</div>;

JSX code appears more like HTML, which can make it easier to read and write, especially for complex UI structures. Under the hood, this JSX code gets compiled to the equivalent 'React.createElement()' code.

Key Differences:

DonMiller9294 commented 1 year ago

Hello thank you for sharing.

phuocng commented 1 year ago

Here it is https://phuoc.ng/collection/this-vs-that/react-create-element-vs-jsx/