.JSX React JSX
.jsx

React JSX

JSX (.jsx) files contain JavaScript with XML-like syntax extensions for writing React UI components. JSX is transpiled to plain JavaScript by tools like Babel or SWC before execution, transforming HTML-like markup into React.createElement() function calls.

بنية الصيغة
Header schema
Records structured data
Source CodeText FormatTranspiledReact2013
بواسطة FileDex
غير قابل للتحويل

Source code format. JSX transpiles to JavaScript, not a file format conversion.

أسئلة شائعة

Is JSX valid JavaScript?

No. JSX is a syntax extension that must be transpiled to plain JavaScript before browsers or Node.js can execute it. Tools like Babel, SWC, or esbuild handle this transformation.

Should I use .jsx or .tsx for new projects?

TSX (TypeScript + JSX) is recommended for most new projects. TypeScript catches prop type errors at compile time, improving reliability and IDE support in larger codebases.

Why use className instead of class in JSX?

Because class is a reserved keyword in JavaScript. JSX maps to JavaScript function calls, so DOM attributes use their JavaScript property names: className, htmlFor, onClick, etc.

ما يميز .JSX

What is a JSX file?

JSX (JavaScript XML) is a syntax extension for JavaScript that allows developers to write HTML-like markup directly within JavaScript code. Created by Facebook (now Meta) for React, JSX is not valid JavaScript by itself — it must be transpiled to regular JavaScript function calls by tools like Babel, SWC, or esbuild before browsers can run it. JSX provides a declarative, readable way to describe UI components and was a key innovation that made React's component model so approachable.

اكتشف التفاصيل التقنية

React without JSX would require writing React.createElement('div', {className: 'box'}, children) for every element. JSX transforms this into the familiar <div className="box">{children}</div> syntax that most developers now consider natural.

How to open JSX files

  • VS Code (Windows, macOS, Linux) — Syntax highlighting, Emmet, IntelliSense
  • WebStorm (Windows, macOS, Linux) — Full React IDE support
  • Vim/Neovim — With React/JSX plugins
  • Any text editor — JSX files are plain UTF-8 text

Technical specifications

Property Value
Language JavaScript + XML-like syntax extension
Transpiler Babel, SWC, esbuild, Vite
Transpiles to Plain JavaScript .js
Runtime Browser (after bundling) or Node.js
Framework React (primary), Preact, Solid.js
MIME type text/jsx

Common use cases

  • React components: Building UI components for web applications
  • React Native: Mobile app development for iOS and Android
  • Server-side rendering: Next.js and Gatsby page components
  • Component prototyping: Rapid UI development and iteration
  • Static site generation: Frameworks like Astro (islands architecture)

JSX syntax example

function UserCard({ name, avatar, role }) {
  return (
    <div className="card">
      <img src={avatar} alt={`${name}'s avatar`} />
      <h2>{name}</h2>
      <p>{role}</p>
      {role === 'admin' && <span className="badge">Admin</span>}
    </div>
  );
}

// Usage
<UserCard name="Alice" avatar="/alice.png" role="admin" />

JSX rules: use className instead of class, htmlFor instead of for, all tags must be closed (<br /> not <br>), and expressions go in {curly braces}.

JSX vs TSX

.jsx uses plain JavaScript with no type checking. .tsx adds TypeScript's static type system. For new projects, TypeScript (.tsx) is recommended — it catches prop type errors at compile time rather than at runtime. For rapid prototyping or smaller projects, .jsx has less boilerplate.

JSX transformation

Babel (or SWC) transforms JSX into JavaScript. With React 17's automatic JSX transform, the output uses _jsx() from react/jsx-runtime instead of React.createElement(). This eliminates the need to import React from 'react' at the top of every file:

// Input JSX
const el = <h1 className="title">Hello</h1>;

// Compiled output (React 17+ automatic transform)
import { jsx as _jsx } from 'react/jsx-runtime';
const el = _jsx('h1', { className: 'title', children: 'Hello' });

React hooks in JSX files

import { useState, useEffect } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    <button onClick={() => setCount(c => c + 1)}>
      Clicked {count} times
    </button>
  );
}

Hooks (useState, useEffect, useCallback, etc.) only work inside function components and must be called at the top level — never inside loops, conditions, or nested functions.

Linting and formatting

JSX projects typically use ESLint with eslint-plugin-react and eslint-plugin-react-hooks to catch common mistakes like missing dependency arrays in useEffect, or using array indexes as React key props. Prettier formats JSX with its standard JavaScript formatter and handles JSX-specific indentation correctly.

المرجع التقني

نوع MIME
text/jsx
المطوّر
Meta (Facebook)
سنة التقديم
2013
معيار مفتوح
نعم

البنية الثنائية

Plain text UTF-8 source code combining JavaScript with XML-like syntax extensions. No binary headers or magic bytes. Files typically begin with import statements (import React from 'react'), followed by function or class component definitions that return JSX markup. JSX angle-bracket syntax is not valid JavaScript and requires transpilation.

2013Facebook open-sources React with JSX syntax, initially criticized for mixing HTML into JavaScript2015Babel (formerly 6to5) becomes the standard JSX transpiler, enabling JSX adoption across the JavaScript ecosystem2017React 16 introduces Fiber reconciler and fragments (<>...</>), reducing unnecessary wrapper elements in JSX2019React 16.8 introduces Hooks (useState, useEffect), shifting JSX patterns from class components to functions2020React 17 automatic JSX transform eliminates the need for 'import React' at the top of every JSX file2024React 19 adds Server Components and Actions, expanding JSX from client-only to full-stack rendering
Transpile JSX with Babel أخرى
npx babel src/ --out-dir dist/ --presets=@babel/preset-react

Transpiles all JSX files in src/ to plain JavaScript in dist/ using the React preset for JSX transformation.

Create a new React app with Vite أخرى
npm create vite@latest my-app -- --template react

Scaffolds a new React project with Vite as the build tool, preconfigured for JSX support.

Run ESLint on JSX files أخرى
npx eslint --ext .jsx,.js src/

Lints all JSX and JS files in the src/ directory for React-specific issues like missing hook dependencies.

Transpile JSX with SWC أخرى
npx swc src/App.jsx -o dist/App.js

Uses SWC (Rust-based compiler) for faster JSX transpilation compared to Babel, with compatible output.

JSX JS render lossless Babel, SWC, or esbuild transpiles JSX syntax into plain JavaScript React.createElement() or _jsx() calls that browsers and Node.js can execute.
JSX BUNDLE (.JS) render lossless Build tools like Vite, Webpack, or esbuild transpile, tree-shake, and bundle JSX files into optimized JavaScript bundles for production deployment.
منخفض

نقاط الضعف

  • XSS via dangerouslySetInnerHTML if user input is passed without sanitization
  • Supply chain attacks via malicious npm packages in React dependency trees
  • Prototype pollution through improper use of spread operators on untrusted objects

الحماية: FileDex does not execute code files. Reference page only.

React مكتبة
Meta's UI library for building component-based interfaces, the primary consumer of JSX syntax
Babel أداة
JavaScript compiler that transforms JSX and modern JS syntax into browser-compatible code
Vite أداة
Fast build tool with native JSX support via esbuild for development and Rollup for production
SWC أداة
Rust-based JavaScript/JSX compiler used by Next.js, offering 20-70x faster transpilation than Babel
Next.js مكتبة
Full-stack React framework with SSR, SSG, and App Router supporting JSX components