app-generator / docs

App Generator - The Official Documentation | AppSeed
https://docs.appseed.us
1 stars 1 forks source link

[React] jsx #108

Open mahfujul-helios opened 1 month ago

mahfujul-helios commented 1 month ago

jsx

what is jsx ?

JSX, short for JavaScript XML, serves as a powerful syntax extension for JavaScript primarily used in the React library. It allows developers to write HTML-like code directly within their JavaScript files, thereby seamlessly integrating markup and logic. This unique blend of JavaScript and XML-like syntax enhances code readability and maintainability, as developers can clearly define the structure of their UI components alongside the corresponding JavaScript logic. JSX facilitates the creation of dynamic and interactive user interfaces by enabling the embedding of JavaScript expressions within curly braces, thereby supporting dynamic content rendering and conditional logic within JSX elements. Furthermore, JSX promotes component-based architecture, fostering the creation of reusable and modular UI components that can be composed together to build complex applications. Overall, JSX plays a fundamental role in modern web development, empowering developers to efficiently create robust and feature-rich applications with React.

how it's work

JSX works by allowing developers to write HTML-like syntax directly within JavaScript code. Under the hood, JSX is transpiled into regular JavaScript code that creates React elements. Here's a high-level overview of how JSX works:

Writing JSX: Developers write JSX syntax directly within their JavaScript code, typically within React components. JSX resembles HTML markup but is actually a syntactic sugar for function calls.

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

Transpilation: When the code containing JSX is compiled or transpiled, tools like Babel are used to convert JSX syntax into regular JavaScript code. This process involves transforming JSX elements into React.createElement() function calls.

const element = React.createElement('h1', null, 'Hello, world!');

Each JSX element is transpiled into a call to React.createElement(), which creates a React element representing the corresponding HTML element.

Creating React Elements: The React.createElement() function takes three arguments: the type of element to create (e.g., 'h1'), any attributes or props to apply to the element (e.g., null in this case), and the children elements or text content of the element (e.g., 'Hello, world!').

Rendering React Elements: Once React elements are created using React.createElement(), they can be rendered to the DOM using ReactDOM's render() function. This process recursively creates and updates the actual DOM elements based on the React element tree.

ReactDOM.render(element, document.getElementById('root'));

Dynamic Content: JSX allows embedding JavaScript expressions within curly braces {}. This enables developers to render dynamic content, execute functions, and evaluate expressions within JSX elements.

const name = 'John';
const element = <h1>Hello, {name}!</h1>;

When this JSX is transpiled, the JavaScript expression {name} is evaluated, and its result is inserted into the resulting React element.

Overall, JSX simplifies the process of defining UI components in React applications by allowing developers to write HTML-like syntax directly within JavaScript, making code more readable and maintainable. Behind the scenes, JSX is transformed into regular JavaScript code that creates and manipulates React elements, facilitating the development of dynamic and interactive user interfaces.

The Rules of JSX

  1. Return a single root element To return multiple elements from a component, wrap them with a single parent tag.

For example, you can use a

:

<div>
  <h1>Hedy Lamarr's Todos</h1>
  <img 
    src="https://i.imgur.com/yXOvdOSs.jpg" 
    alt="Hedy Lamarr" 
    class="photo"
  >
  <ul>
    ...
  </ul>
</div>

If you don’t want to add an extra

to your markup, you can write <> and </> instead:

<>
  <h1>Hedy Lamarr's Todos</h1>
  <img 
    src="https://i.imgur.com/yXOvdOSs.jpg" 
    alt="Hedy Lamarr" 
    class="photo"
  >
  <ul>
    ...
  </ul>
</>

some example

import React from 'react';

const MyComponent = () => {
  const name = 'John';
  const age = 30;

  return (
    <div>
      <h1>Hello, {name}!</h1>
      <p>You are {age} years old.</p>
    </div>
  );
};

export default MyComponent;

In this example:

  • We import the React library, which is necessary whenever JSX is used.
  • We define a functional component called MyComponent.
  • Inside the component, we declare two variables: name and age.
  • We return a JSX expression that represents the structure of the component.
  • JSX allows us to embed JavaScript expressions within curly braces {}. In this case, we use {name} and {age} to insert dynamic content into the JSX elements.
  • Finally, we export the MyComponent function so that it can be imported and used in other parts of the application.

When this JSX code is transpiled into regular JavaScript, it would look something like this:

const MyComponent = () => {
  const name = 'John';
  const age = 30;

  return React.createElement('div', null,
    React.createElement('h1', null, 'Hello, ', name, '!'),
    React.createElement('p', null, 'You are ', age, ' years old.')
  );
};

export default MyComponent;

As you can see, JSX provides a more concise and readable way to define the structure of React components, making it easier to work with complex UIs and dynamic data.

conclusion

JSX (JavaScript XML) is a syntax extension for JavaScript commonly used with React. It allows developers to write HTML-like code directly within JavaScript, making it easier to define the structure of React components. JSX simplifies the process of creating user interfaces by providing a familiar and expressive syntax, reminiscent of HTML, for describing the UI elements and their relationships. By allowing the embedding of JavaScript expressions within curly braces {}, JSX enables dynamic content rendering and facilitates the use of variables, functions, and conditional logic within the UI. Overall, JSX enhances the readability, maintainability, and efficiency of React code, empowering developers to build complex and interactive web applications with ease.