|
| 1 | +--- |
| 2 | +title: "Overview" |
| 3 | +order: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +[TODO: Review] |
| 7 | + |
| 8 | +:::note |
| 9 | +While this guide can be helpful for understanding reactive systems, it does use some Solid-specific terminology and concepts. |
| 10 | +::: |
| 11 | + |
| 12 | +Reactivity is what powers the interactivity of Solid apps. |
| 13 | +This programming paradigm refers to a system's ability to respond to changes in data, or state, automatically and efficiently. |
| 14 | +Solid is built with reactivity at the core of its design, assisting applications with staying up-to-date with its underlying data. |
| 15 | + |
| 16 | +## The Importance of Reactivity |
| 17 | + |
| 18 | +Reactivity is what keeps the user interface (UI) in sync with the underlying application state. |
| 19 | +When the state changes, the UI is automatically updated, reducing the need for manual updates. |
| 20 | + |
| 21 | +In addition, reactivity enables real-time updates, allowing applications to reflect changes instantly without requiring a full page refresh. |
| 22 | +This helps with creating more responsive and interactive user experiences. |
| 23 | + |
| 24 | +As an example, when building a Counter, you can use the reactive primitives provided by Solid to create a counter. |
| 25 | +Once the counter is set up, whenever the count changes, the UI will automatically update to reflect the new count: |
| 26 | + |
| 27 | +```tsx |
| 28 | +function Counter() { |
| 29 | + const [count, setCount] = createSignal<number>(0); |
| 30 | + const increment = () => setCount((prev) => prev + 1); |
| 31 | + |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <span>Count: {count()}</span>{" "} |
| 35 | + {/* Only `count()` is updated when the button is clicked. */} |
| 36 | + <button type="button" onClick={increment}> |
| 37 | + Increment |
| 38 | + </button> |
| 39 | + </div> |
| 40 | + ); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Reactive Principles |
| 45 | + |
| 46 | +### Signals |
| 47 | + |
| 48 | +Signals serve as the core elements within a reactive system. |
| 49 | +They play a crucial role in tracking and managing state changes, allowing the UI to respond automatically when the underlying data changes. |
| 50 | +They are responsible for storing and managing data, as well as triggering updates across the system. |
| 51 | + |
| 52 | +Signals are able to achieve this reactivity through the use of: |
| 53 | + |
| 54 | +- **Getters**: A function responsible for retrieving the current value of a signal. |
| 55 | +When called within a reactive context, it will give access to the current value of the signal. |
| 56 | +- **Setters**: This function is responsible for updating the value of a signal. |
| 57 | +To trigger reactivity, setters notify the system that the signal's value has changed, prompting anything that depends on the signal to re-evaluate and update accordingly. |
| 58 | + |
| 59 | +<EraserLink |
| 60 | + href="https://app.eraser.io/workspace/maDvFw5OryuPJOwSLyK9?elements=lseAEjGlKLslaVsTlfej_g" |
| 61 | + preview="https://app.eraser.io/workspace/maDvFw5OryuPJOwSLyK9/preview?elements=lseAEjGlKLslaVsTlfej_g&type=embed" |
| 62 | +/> |
| 63 | + |
| 64 | +### Subscribers |
| 65 | + |
| 66 | +Subscribers refer to other reactive contexts or components that depend on the value of a signal. |
| 67 | +These automated responders keep the system up-to-date by re-evaluating and updating whenever the signal's value changes. |
| 68 | + |
| 69 | +Subscribers work based on two main actions: |
| 70 | +- **Observation**: The core function of a subscriber is to observe signals. |
| 71 | +This keeps the subscriber informed about any changes to the signal(s) they're tracking. |
| 72 | +- **Respond**: Once the signal has updated and the subscriber is notified, it triggers a re-evaluation of the dependent computations or UI updates. |
| 73 | + |
| 74 | +[TODO: Some kind of image / code block??] |
0 commit comments