ReasonJun

React : JSX (JavaScript XML) 본문

Frontend/React

React : JSX (JavaScript XML)

ReasonJun 2023. 6. 11. 00:31
728x90

JSX (JavaScript XML) is an extension to the JavaScript language syntax used in React. It allows you to write HTML-like code within JavaScript, making it easier to define the structure and appearance of UI components in React.

 

With JSX, you can write HTML-like tags and include JavaScript expressions within curly braces {}. It provides a concise and intuitive way to define the UI hierarchy and the data bindings within the component.

 

Here's an example of JSX code in React:

import React from 'react';

const App = () => {
  const name = 'John Doe';
  const greeting = <h1>Hello, {name}!</h1>;

  return (
    <div>
      {greeting}
      <p>Welcome to my React app.</p>
    </div>
  );
};

export default App;

In the above code, we define a functional component named App. Within the component's return statement, we use JSX to define the UI structure. We can use HTML-like tags such as <div>, <h1>, <p>, etc., to create the desired HTML structure.

 

JSX also allows us to include JavaScript expressions within curly braces {}. In the example, we use the name variable to display a dynamic greeting message by embedding it within the <h1> tag.

 

Under the hood, JSX code is transformed into regular JavaScript function calls by a transpiler like Babel. The transpiler converts JSX tags and expressions into React createElement calls, creating a virtual representation of the UI components.

 

Using JSX provides several benefits in React development:

  1. Familiar Syntax: JSX resembles HTML, making it easier for developers with web development background to adopt React.
  2. Readability: JSX code is more readable and expressive compared to manipulating the DOM directly with JavaScript.
  3. Component Structure: JSX allows for nesting and composing components in a declarative manner, making it easier to understand the hierarchy and relationships between components.
  4. JavaScript Integration: JSX seamlessly integrates JavaScript expressions, allowing you to dynamically generate UI components based on data and logic.
  5. Compile-Time Safety: JSX code is checked and validated at compile-time, providing early detection of syntax errors and potential issues.

Overall, JSX enhances the development experience in React by combining the power of JavaScript with a familiar and intuitive syntax for defining UI components and their behavior.

728x90
Comments