|
| 1 | +--- |
| 2 | +title: "Basics of Components" |
| 3 | +order: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +Components are the **fundamental building blocks** of Solid applications. |
| 7 | +A component is a **JavaScript function that returns JSX**. |
| 8 | +This allows you to create encapsulated pieces of UI that can manage their own state and behavior. |
| 9 | + |
| 10 | +## Your First Component |
| 11 | + |
| 12 | +A component starts as a plain JavaScript function, defined by either a function declaration or an arrow function: |
| 13 | + |
| 14 | +```tsx |
| 15 | +// Greeting.jsx |
| 16 | +export function Greeting() { |
| 17 | + return <div>Hello, Solid!</div>; |
| 18 | +} |
| 19 | + |
| 20 | +export const Greeting = () => { |
| 21 | + return <div>Hello, Solid!</div>; |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +These functions can be used in other components by importing them and using their names like HTML tags: |
| 26 | + |
| 27 | +```tsx |
| 28 | +// App.tsx |
| 29 | +import { Greeting } from './Greeting'; |
| 30 | + |
| 31 | +function App() { |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <Greeting /> |
| 35 | + </div> |
| 36 | + ); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Passing Props |
| 41 | + |
| 42 | +Props, short for properties, let you pass data from a parent component to a child component. |
| 43 | +They are passed as attributes on the component tag and received as an object in the component function. |
| 44 | + |
| 45 | +```tsx |
| 46 | +//Greeting.tsx |
| 47 | +export function Greeting(props) { |
| 48 | + return <div>Hello, {props.name}!</div>; |
| 49 | +} |
| 50 | + |
| 51 | +// App.tsx |
| 52 | +import { Greeting } from './Greeting'; |
| 53 | + |
| 54 | +function App() { |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <Greeting name="Solid" /> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +To learn more about props, see the next section: [Passing Data with Props](/components/props). |
| 64 | + |
| 65 | +### Composing Components |
| 66 | + |
| 67 | +Components can be composed together to build more complex UIs. |
| 68 | +You can use one component inside another, passing props as needed. |
| 69 | + |
| 70 | +```tsx |
| 71 | +// App.tsx |
| 72 | +import { Greeting } from './Greeting'; |
| 73 | + |
| 74 | +function App() { |
| 75 | + return ( |
| 76 | + <div> |
| 77 | + <Greeting name="Solid" /> |
| 78 | + <Greeting name="World" /> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Component lifecycles |
| 85 | + |
| 86 | +Components in Solid have a lifecycle that includes mounting, updating, and unmounting phases. |
| 87 | + |
| 88 | +1. **Mounting**: When the component is being created and inserted into the DOM. |
| 89 | +It occurs once in the component's lifecycle. |
| 90 | +2. **Updating**: The component's state or props change, causing it to re-render. Solid optimizes updates to ensure minimal re-rendering. |
| 91 | +3. **Unmounting**: The component is removed from the DOM. |
| 92 | +Cleanup tasks, such as cancelling network requests or removing event listeners, should be performed here. |
| 93 | + |
| 94 | +There are certain lifecycle helpers available in Solid to manage these phases more easily, such as `onMount`, `onCleanup`, and `createEffect`. |
| 95 | + |
| 96 | +```tsx |
| 97 | +import { onMount, onCleanup } from "solid-js"; |
| 98 | + |
| 99 | +function Timer() { |
| 100 | + let interval; |
| 101 | + |
| 102 | + onMount(() => { |
| 103 | + interval = setInterval(() => console.log("tick"), 1000); |
| 104 | + }); |
| 105 | + |
| 106 | + onCleanup(() => clearInterval(interval)); |
| 107 | + |
| 108 | + return <p>Timer running...</p>; |
| 109 | +} |
| 110 | +``` |
0 commit comments