Tutorial
jsx

What is JSX ?

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Introducing JSX

Let's start with a simple example

Consider this variable declaration:

const element = <h1>Hello, world!</h1>;

This funny tag syntax is neither a string nor HTML. JSX stands for JavaScript XML, and it is a syntax extension for JavaScript. It allows you to write HTML-like elements in your JavaScript code, which can then be rendered on a webpage.

Before we dive into JSX, let's talk about index.js
Can you explain what is inside index.js 😥❓
index.js
  import React from 'react';
  import ReactDOM from 'react-dom/client';
  import './index.css';
  import App from './App';
  import reportWebVitals from './reportWebVitals';
 
  const root = ReactDOM.createRoot(document.getElementById('root'));
  root.render(
  <React.StrictMode>
      <App />
  </React.StrictMode>
  );
 
  reportWebVitals();
First talk about import
👀

Why we are use import here ?

We import libraries and modules in JavaScript to use their additional functionality in our code. In this specific code, we import React and ReactDOM to use their functions and methods to build and render a React application, import './index.css' for styling and import App and reportWebVitals functions for the main component and for collecting performance metrics respectively. This way we don't have to build everything from scratch and it makes our code more maintainable.

import React from 'react';:

This line is importing the React library, which is necessary for building React applications. React provides the tools to create and manage components, which are the building blocks of a React application.

Const Root Explain

const root = ReactDOM.createRoot(document.getElementById('root'));

  • The first line is creating a root container element using the ReactDOM.createRoot() method.
  • The createRoot() method creates a new root container that can be used to render multiple components on the webpage. It takes as argument the reference to an existing DOM node, in this case, the element with an id of 'root' from the HTML file. It creates a new root container by passing the element with an id 'root' from the HTML file.

In simpler terms, ReactDOM.createRoot() method creates a special container that holds multiple React components, it uses the element with the id 'root' from the HTML file as the spot to place the container and make the components visible on the webpage.

example

Change the image size by using the click-and-scroll function.

You can use document.getElementById('root') to get a reference to that element, which can be used to manipulate it later with JavaScript. In this case, it's being passed to ReactDOM.createRoot() to create a root container that can be used to render multiple components on the webpage.

root.render( <React.StrictMode> <App /> </React.StrictMode>);:

React.StrictMode is a tool that helps you to find and fix problems in your React application. It's like a helper that checks your code for potential issues and let you know about them. It's mainly used during development and it is not necessary in production.

You can think of it like a "safety net" for your application, it helps you to make sure that everything is working as expected, and it helps you to improve the quality of your code.

In the specific code, React.StrictMode is used to wrap the main App component. When the App component is rendered inside the root container wrapped by React.StrictMode, React will check for potential problems in the application and provide additional warnings to help you to fix them.

In summary, the ReactDOM.createRoot() method is used to create a root container that can be used to render multiple components on the webpage and the render() method of the root element is used to render the App component wrapped in a React.StrictMode on the webpage.

reportWebVitals();: This line is calling the reportWebVitals() function, which can be used to collect and report performance metrics of the application.

🚫

if you use divinstead of React.StrictMode or <> then it will still render the App component but it will not have the benefits of Strict mode, like highlighting potential problems in the application.

Let's understand JSX in detail

Definition: JSX is a syntax extension for JavaScript that allows you to write HTML-like elements and components in your JavaScript code.

In simple words, JSX is a combination of JavaScript and HTML. It is a syntax extension for JavaScript that allows you to write HTML-like elements and components in your JavaScript code.

const element = <h1>Hello, World!</h1>;
ReactDOM.render(element, document.getElementById('root'));

In this example, const element is a JSX element that represents an <h1> tag with the text "Hello, World!". The ReactDOM.render() function is used to render the JSX element on the webpage, and the document.getElementById('root') specifies where in the HTML the element should be rendered.

⭐ Experiment

Let's experiment with JSX

Don't worry this is a very simple experiment and you will understand it very easily.

Step 1

Let's create a variable and assign it in JSX format.

Open src/App.js and add the following code to it:

let name = "Subham"
 
function App() {
  return (
      <>
        <div className="container">
          <h1>Hello {name}</h1>
         </div>
      </>
  );
}
 
export default App;
example

Change the image size by using the click-and-scroll function.

Step 2

Now open your terminal and run the following command:

npm start

⚡ Playground

👀

some key points to remember:

  • Use lowercase for HTML elements, like <div>, <p>, and <h1>.
  • Use camelCase for custom components, like <MyComponent>.
  • Use className instead of class to define CSS classes.
  • Use htmlFor instead of for to define labels for form elements.
  • Use self-closing tags for elements that don't have any children, like <img/>.
  • Nest elements inside of other elements to create a hierarchy, like <div><p>Hello</p></div>.
  • Use curly braces {} to include JavaScript expressions or variables within JSX, like <p>{variable}</p>.
  • Always close tags, even if they are self-closing, like <img />
  • JSX should always have a parent element, so it should not return multiple element, if so, it should be wrapped in a div or fragment like <> or React.Fragment
  • If you want to return two elements using JSX you have to use the JSX Fragment
  • Feature or wrap the whole content in one element.

⭐ Function & Class Components

Explain Function & Class Components

In React, there are two main ways to create a component: function-based components and class-based components.

A function-based component is a JavaScript function that returns JSX (HTML-like elements and components) to be rendered on the page. These components are simple, easy to understand and read. They are defined using the function keyword and they are also known as stateless components. Here is an example of a simple function-based component:

    function Welcome(props) {
        return <h1>Hello, {props.name}</h1>;
    }

A class-based component, on the other hand, is a JavaScript class that extends the React.Component class and has a render() method. The render() method returns JSX to be rendered on the page. These components are a bit more complex than function-based components as they can have their own internal state and lifecycle methods. They are defined using the class keyword and they are also known as stateful components.

Here is an example of a simple class-based component:

    class Welcome extends React.Component {
      render() {
        return <h1>Hello, {this.props.name}</h1>;
    }
  }

Nowadays, function-based components are primarily used and recommended to be used. The main reason is that they are simpler and easier to understand, and they also have better performance than class-based components.

It's good to know both the ways of creating a component in React, but it's recommended to use function-based components unless you need to use the additional features that class-based components provide.

⭐ Quick MCQs

Quick Question no 1

  1. What is JSX?

Answer

Answer: B. JSX is a syntax extension for JavaScript that allows you to write HTML-like elements in your JavaScript code. It is commonly used in React to create reusable components.

Quick Question no 2

  1. How is JSX different from HTML?

Answer

Answer: B. JSX is a syntax extension for JavaScript, so it allows you to include JavaScript expressions directly in your HTML-like elements. This allows you to create dynamic and interactive components that can change in response to data and state. HTML, on the other hand, is a markup language and does not support JavaScript expressions.

Quick Question no 3

  1. What is the purpose of JSX?

Answer

Answer: C. The purpose of JSX is to make it possible to write HTML and CSS in JavaScript, which allows for more powerful and dynamic user interfaces. It also makes it easier to create reusable components in React.

Quick Question no 4

  1. What is the difference between JSX and JavaScript?

Answer

Answer: B. JSX is a syntax extension for JavaScript, while JavaScript is a programming language. JSX allows you to write HTML-like elements in your JavaScript code, making it easier to create reusable components in React.

Quick Question no 5

  1. What is the purpose of the React.createElement() function?

Answer

Answer: c. To create an element that can be rendered by React

Explanation: The React.createElement() function is used to create a virtual element that can be rendered by React. It takes three arguments: the type of the element (e.g. "div"), an object containing props, and any children elements. This function is used internally by React to create elements from JSX code, but it can also be used directly to create elements without using JSX.

Quick Question no 6

  1. How is JSX transpiled?

Answer

Answer: A. JSX is transpiled, or converted, by using a transpiler like Babel. This allows JSX to be understood by older browsers or JavaScript engines that do not support it natively.

Quick Question no 7

  1. Can JSX be used outside of React?

Answer

Answer: A. JSX can be used outside of React, it can be used with any JavaScript framework or library that support it, although it is most commonly used with React.