Dung (Donny) Nguyen

Senior Software Engineer

JSX

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows us to write HTML-like code within our JavaScript code. JSX is used in React.js to describe the UI components and their structure in a more readable and declarative manner. Here’s why it’s so valuable:

Key Features of JSX:

Example of JSX:

Here’s a simple example of how JSX is used in a React component:

import React from 'react';

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

export default Greeting;

In this example, the Greeting component uses JSX to render an h1 element with a dynamic greeting message based on the props.name value.

Why Use JSX in React.js?

  1. Declarative Syntax: JSX allows us to describe what the UI should look like in a declarative manner. This aligns with React’s philosophy of making the UI predictable and easier to reason about.
  2. Component Structure: JSX helps in defining the structure of components in a clear and hierarchical way, making it easier to manage and maintain complex UIs.
  3. Integration with JavaScript: Since JSX is ultimately transformed into JavaScript, it seamlessly integrates with all JavaScript features and functionalities, making it powerful and flexible.
  4. Tooling and Ecosystem: JSX is widely supported by tools in the React ecosystem, including code editors, linters, and build tools, making the development experience smooth and efficient.

JSX is not mandatory, but it is highly recommended for React development due to its numerous benefits in terms of readability, maintainability, and productivity.

To use JSX in React.js, we need to ensure a few prerequisites are met. Here’s what we need:


1. A React Setup

JSX is part of React, so we need a React environment set up. There are several ways to set up a React project:


2. A JSX Compiler (Babel)

Browsers don’t understand JSX natively. We need a compiler like Babel to transform JSX into standard JavaScript that browsers can execute. Babel is typically included in React setups like CRA or frameworks such as Next.js.


3. ES6+ JavaScript Knowledge

JSX works seamlessly with modern JavaScript (ES6+). Familiarity with features like:


4. A Module Bundler (Optional but Common)

JSX is usually used with bundlers like Webpack or Vite. They handle file imports, bundling, and integration with Babel.


5. A Code Editor

We can use any code editor, but Visual Studio Code is highly recommended for its React.js extensions and syntax highlighting for JSX.


Example Setup

Here’s what a minimal manual setup for using JSX might look like:

  1. Install React, Babel, and Webpack.
  2. Configure Babel to transpile JSX.
  3. Create a React component:
    import React from 'react';
    import ReactDOM from 'react-dom';
    
    const App = () => <h1>Hello, React with JSX!</h1>;
    
    ReactDOM.render(<App />, document.getElementById('root'));
    
  4. Run the bundler to serve the app.

By meeting these prerequisites, we’ll have an environment ready to use JSX effectively!