|
| 1 | +--- |
| 2 | +title: "Understanding JSX" |
| 3 | +order: 2 |
| 4 | +--- |
| 5 | + |
| 6 | +Solid's JSX is a JavaScript extension that lets you write **HTML-like syntax inside JavaScript**. |
| 7 | +It keeps your rendering logic and markup together, making components easier to read and maintain. |
| 8 | + |
| 9 | +## How Solid uses JSX |
| 10 | + |
| 11 | +Solid was designed to align closely with HTML standards. |
| 12 | + |
| 13 | +```tsx |
| 14 | +const element = <h1>I'm JSX!!</h1> |
| 15 | +``` |
| 16 | + |
| 17 | +Unlike some other frameworks, Solid compiles JSX directly to real DOM nodes. |
| 18 | +This means: |
| 19 | +- JSX expressions map closely to HTML. |
| 20 | +- You can use variables and functions inline using curly braces `{}`. |
| 21 | +- Only the parts of the DOM that depend on reactive state update. |
| 22 | +This avoids the whole component re-rendering, improving performance. |
| 23 | + |
| 24 | +```tsx |
| 25 | +const component = () => { |
| 26 | + const animal = { breed: "cat", name: "Midnight" } |
| 27 | + |
| 28 | + return ( |
| 29 | + <p> |
| 30 | + I have a {animal.breed} named {animal.name}. |
| 31 | + </p> |
| 32 | + ) |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Solid's JSX Rules |
| 37 | + |
| 38 | +### 1. Return a Single Root Element |
| 39 | + |
| 40 | +Each component must return a single root element. |
| 41 | +Since JSX maintains the familiar tree-like structure of HTML, having a single root element helps keep the hierarchy clear. |
| 42 | +If you need multiple top-level elements, wrap them in a `<div>`, `<section>`, or use a fragment `<>...</>`. |
| 43 | + |
| 44 | +```tsx |
| 45 | +function App() { |
| 46 | + return ( |
| 47 | + <div> |
| 48 | + <h1>Hello World!</h1> |
| 49 | + <p>Welcome to JSX in Solid.</p> |
| 50 | + </div> |
| 51 | + ) |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +```advanced |
| 56 | +JSX in Solid compiles into structured HTML. |
| 57 | +Static elements are optimized once, while dynamic ones get special markers so Solid can update them efficiently. |
| 58 | +Requiring a single root element keeps the hierarchy consistent and easier to update. |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | +### 2. Close All Tags |
| 63 | + |
| 64 | +All tags must be properly closed. |
| 65 | +This includes self-closing tags like `<img />`, `<input />`, and `<br />`. |
| 66 | + |
| 67 | +```tsx |
| 68 | +// Correct |
| 69 | +<img src="image.jpg" alt="Description" /> |
| 70 | + |
| 71 | +// Incorrect |
| 72 | +<img src="image.jpg" alt="Description"> |
| 73 | +``` |
| 74 | + |
| 75 | +### 3. Properties vs Attributes |
| 76 | + |
| 77 | +In Solid's JSX, you can use both HTML attributes and JSX properties (props) to define element behavior and appearance. |
| 78 | + |
| 79 | +#### HTML attributes |
| 80 | + |
| 81 | +Solid's JSX blends HTML attributes with JavaScript expressions. |
| 82 | + |
| 83 | +- **HTML attributes** → work similar to regular HTML, but you can use JavaScript expressions inside curly braces `{}`. |
| 84 | +- **Event listeners** → Solid's JSX allows you to add event listeners using camelCase (e.g., `onClick`, `onChange`) or all lowercase (e.g., `onclick`, `onchange`). |
| 85 | +- **Inline styles** → You can apply styles directly using the `style` attribute with a JavaScript object. |
| 86 | + |
| 87 | +```tsx |
| 88 | +<button class="myClass" onClick={handleClick}> |
| 89 | + Click me! |
| 90 | +</button> |
| 91 | + |
| 92 | +<button style={{ color: "red", fontSize: "2rem" }}> |
| 93 | + Styled Button |
| 94 | +</button> |
| 95 | +``` |
| 96 | + |
| 97 | +You can also use JavaScript expressions to set attribute values dynamically: |
| 98 | + |
| 99 | +```tsx |
| 100 | +const isActive = true; |
| 101 | + |
| 102 | +<button class={isActive ? "active" : "inactive"}> |
| 103 | + Click me! |
| 104 | +</button> |
| 105 | +``` |
| 106 | + |
| 107 | +For more information on: |
| 108 | +- [Using Event Listeners](/components/how-to/event-listeners) |
| 109 | +- [Styling Components](/components/how-to/styling) |
| 110 | + |
| 111 | +#### JSX Properties (Props) |
| 112 | + |
| 113 | +JSX properties (or props) are how you pass data and event handlers to components in Solid. |
| 114 | +They work similarly to HTML attributes but can accept JavaScript expressions. |
| 115 | + |
| 116 | +In this example, the `name` prop is passed to the `Greeting` component, which uses it within its JSX: |
| 117 | + |
| 118 | +```tsx |
| 119 | +function App() { |
| 120 | + return ( |
| 121 | + <div> |
| 122 | + <Greeting name="Alice" /> |
| 123 | + <Greeting name="Bob" /> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +function Greeting(props) { |
| 129 | + return <h1>Hello, {props.name}!</h1>; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +The core concepts of JSX properties in Solid include: |
| 134 | +- **Static props**: Directly integrated into the HTML by cloning the template and using them as attributes. |
| 135 | +- **Dynamic props**: Rely on state, allowing content or properties to change in response to user interactions or other events. |
| 136 | +An example is changing the style of an element based on a signal (`value={value()}`). |
| 137 | +- **Data transfer**: Props can be used to fill components with data from resources, such as API responses or context providers. |
| 138 | +This allows for components to update reactively when the underlying data changes. |
| 139 | + |
| 140 | +:::note |
| 141 | +In JSX, expressions are applied in the order they appear. |
| 142 | +This works for most elements, but some (i.e. `<input type="range" />`) require attributes in a specific order. |
| 143 | +When order matters, make sure to define expressions in the sequence the element expects. |
| 144 | +::: |
| 145 | + |
| 146 | +## Related Pages |
| 147 | +- [Using Event Listeners](/components/how-to/event-listeners) |
| 148 | +- [Styling Components](/components/how-to/styling) |
| 149 | +- [Passing Data with Props](/components/props) |
0 commit comments