> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/react/jsx.md).

# JSX

Need to import react to use

* Combines logic with display
* Can put **any** js expression within braces in JSX
* React elements are immutable js objects
* its elements are just JavaScript objects, you could potentially look at and clone with changes
* ```jsx
  const name = 'Josh Perez';
  const element = <h1>Hello, {name}</h1>;

  const element = <img src={user.avatarUrl} />;
  //either use quote string or js expression
  ```

  *auto esapes values in JSX to prevent XSS*

## Syntax

* Can close empty tags with /> i.e `<img src=... />`
* Camel case:
  * class => className
  * tabindex => tabIndex
* Can have nested divs, but must have a single outermost div or fragments
* Can be multiple lines

#### JSX transform

```jsx
const element = (
  <h1 className="greeting">
    Hello, world!
  </h1>
);
//jsx becomes
const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, world!'
);
//which essentially becomes
const element = {
  type: 'h1',
  props: {
    className: 'greeting',
    children: 'Hello, world!'
  }
};
```

### Rendering with Variable

```jsx
let button = <LogoutButton onClick={this.handleLogoutClick} />;
return (  <div>
        <Greeting isLoggedIn={isLoggedIn} />
        {button}
      </div>
    );
```

### Inline Logic

```jsx
function Mailbox(props) {
  const unreadMessages = props.unreadMessages;
  return (
    <div>
      <h1>Hello!</h1>
      {unreadMessages.length > 0 &&
        <h2>
          You have {unreadMessages.length} unread messages.
        </h2>
      }
    </div>
  );
}
```

```jsx
render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.
    </div>
  );
}
```

### Prevent Component from Rendering

just return null

```jsx
function WarningBanner(props) {
  if (!props.warn) {
    return null;
  }

  return (
    <div className="warning">
      Warning!
    </div>
  );
}
```

## Fragments

```jsx
class Columns extends React.Component {
  render() {
    return (
      <>
        <td>Hello</td>
        <td>World</td>
      </>
    );
  }
}
```

##


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/react/jsx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
