JSX: Consider the following code snippet,
const sample = <h2>Greetings</h2>;
The above code snippet somewhat looks like HTML, and it also uses a JavaScript-like variable but is neither HTML nor JavaScript, it is JSX. The JSX is basically a syntax extension of regular JavaScript and is used to create React elements. These elements are then rendered to the React DOM. Each JSX element is just to make use of React easy and for calling React.createElement(component, props, ...children) with less work. So, anything that is done with JSX can also be done with just plain JavaScript.
Most people use JSX with react, but that requires Babel which converts the ES6 code to a code that is compatible with the browsers. It means we need to a something like webpack. If we don't use JSX then we don't have to worry any about that.
JSX-less approach over the JSX-focused React Application: It is advisable to stick to JSX for your medium-sized or bigger projects. It still helps to understand what happens under the hood. For smaller React projects, where you don’t want to add a complex build workflow, or for multi-page-applications, you could consider the JSX-less version though.
Example 1: Using React without JSX using CDN (without Node). Following is a simple Html code to print Hello World using React without JSX.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script src=
"https://unpkg.com/react@16/umd/react.development.js">
</script>
<script src=
"https://unpkg.com/react-dom@16/umd/react-dom.development.js">
</script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
var e = React.createElement;
ReactDOM.render(
e('h1', null, 'Hello, world!'),
document.getElementById('root')
);
</script>
</body>
</html>
Output:
Output of the above program
Example 2: Using React without JSX in Application.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app rwjsx
Step 2: After creating your project folder i.e. rwjsx, move to it using the following command:
cd rwjsx
Project Structure: It will look like the following.
Project Structure
Now write down the following code in the index.js file in the src folder after all import statements.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
ReactDOM.render(
React.createElement(Hello, {toWhat: 'World'}, null),
document.getElementById('root')
);
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
React.js without ES6 Reactjs is the best frontend library ever created. It is made by Facebook to perform several tasks in the frontend itself. ES6 is the standardization of javascript for making code more readable and more accessible. If we don't use ES6 in react, there is an alternative to perform. We use create-react
5 min read
Why does React use JSX? React uses JSX (JavaScript XML) as a syntax extension for JavaScript. JSX is a syntax extension that looks similar to XML or HTML but is designed to work seamlessly with JavaScript. Reasons why React uses JSX:Readability and Expressiveness: JSX provides a more concise and readable syntax for definin
2 min read
What is React? React JS is a free library for making websites look and feel cool. It's like a special helper for JavaScript. People from Facebook and other communities work together to keep it awesome and up-to-date. React is Developed by Facebook, React is a powerful JavaScript library used for building user inte
6 min read
React JSX JSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
6 min read
Next.js vs Nuxt.js Frameworks and libraries are constantly evolving to meet the needs of developers and businesses. Two of the most popular frameworks for building server-side rendered (SSR) applications are Next.js and Nuxt.js. Both frameworks offer great features and capabilities, but they cater to different ecosyst
4 min read
Shortest React App In this article, we will discuss about how to create the shortest React app. It will have the fewest files possible, making it easy to learn the basics of React. Prerequisites:React JSHTML, CSS, and JavaScriptTable of Content What is React JS? Steps to Create a React App Using CDN MethodWhy are ther
3 min read