Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Understanding JSX | Introduction to React
/
React Essentials: From Fundamentals to Components

bookUnderstanding JSX

Deslize para mostrar o menu

JSX stands for JavaScript XML, and it is a syntax extension for JavaScript that you use to describe what the UI should look like in React applications. JSX lets you write code that looks very similar to HTML, but you can also embed JavaScript expressions directly within this markup. This approach makes it much easier to create and visualize user interfaces, as you can mix markup and logic in a way that feels natural and readable. Instead of separating HTML and JavaScript into different files, JSX allows you to combine them, letting you write components that are both expressive and maintainable.

When you write JSX, you are not writing standard JavaScript. Browsers do not understand JSX directly. Instead, tools like Babel transpile your JSX code into regular JavaScript function calls. For example, a simple JSX element like <h1>Hello, world!</h1> is converted into React.createElement('h1', null, 'Hello, world!'). This means that while JSX looks like HTML, it is actually just syntactic sugar for creating React elements. You can also embed JavaScript expressions inside JSX by wrapping them in curly braces {}. For example, you might write <p>{userName}</p>, which will render the value of the userName variable. However, you cannot embed statements like if or for directly inside JSX; only expressions are allowed.

When working with JSX, there are some common pitfalls and best practices you should keep in mind.

  • Remember that JSX elements must have a single parent element;
  • If you want to return multiple elements from a component, wrap them in a parent element like a div or use a React Fragment (<>...</>);
  • Attribute names in JSX use camelCase instead of HTML attribute names; for example, use className instead of class, and htmlFor instead of for;
  • Always close your tags, even for elements that do not require a closing tag in HTML, such as img or input.
  • Following these rules helps avoid errors and keeps your code consistent and readable.
question mark

Which statement best describes JSX in React?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 2
some-alt