Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1.77 KB

04.JSX.md

File metadata and controls

40 lines (27 loc) · 1.77 KB

The Power of JSX: How It Makes Building Components in React a Breeze

In this blog post, we'll take a deep dive into JSX, the syntax extension used in React for defining components and rendering UI. We'll explore how JSX works with components in React, and how it allows you to write concise and expressive code for building web applications.

What are JSX?

"JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code in your JavaScript files. It is essentially just HTML written in a JS file, and all the HTML is ultimately converted to XML. In other words, JSX is a sugar-coated version of HTML tags. You can see the underlying code in the main.js file through developer tools.

In JSX components, there must be only one root element returned, not multiple root elements. It is important to note that JSX is not actually HTML, but rather a syntax developed by the React team to resemble HTML. If you need to add classes, you should use the className attribute instead of the class attribute."

There are a few things to keep in mind when working with JSX:

  • You must wrap your JSX elements in a parent element, or you will get a syntax error.

  • You can use JavaScript expressions within JSX by enclosing them in curly braces.

  • You can use JSX elements as the children of other JSX elements, allowing you to build complex UI from smaller, reusable pieces.

  • You can use the React.createElement function to create JSX elements dynamically at runtime.

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

const element = (
  <div>
    <h1>Hello, World!</h1>
    <p>This is a paragraph</p>
  </div>
);

const element = <MyComponent name="John" />;

const element = (
  <ul>
    {items.map((item) => (
      <li key={item.id}>{item.name}</li>
    ))}
  </ul>
);