|
| 1 | +--- |
| 2 | +title: Building for mobile |
| 3 | +category: Guides |
| 4 | +order: 7 |
| 5 | +use_cases: >- |
| 6 | + mobile apps, ios, android, capacitor, tauri, nativescript, webview, custom |
| 7 | + renderers, native views |
| 8 | +tags: |
| 9 | + - mobile |
| 10 | + - capacitor |
| 11 | + - tauri |
| 12 | + - nativescript |
| 13 | + - webview |
| 14 | + - solid-js/universal |
| 15 | + - createRenderer |
| 16 | + - native |
| 17 | +version: "1.0" |
| 18 | +description: >- |
| 19 | + Run Solid on iOS and Android with a WebView shell (Capacitor, Tauri), |
| 20 | + NativeScript, or a custom renderer built on solid-js/universal. |
| 21 | +--- |
| 22 | + |
| 23 | +Three routes get a Solid app onto iOS and Android: |
| 24 | + |
| 25 | +- **WebView shell:** a native app embeds a browser engine (a WebView) that runs your web build. Use [Capacitor](https://capacitorjs.com/) or [Tauri](https://v2.tauri.app/). |
| 26 | +- **DOM shim over native views:** Solid renders to real native views through a DOM-like layer. Use [NativeScript](https://nativescript.org/). |
| 27 | +- **Custom renderer:** you implement a renderer with `solid-js/universal` and drive native views directly. |
| 28 | + |
| 29 | +Solid's reactivity is the same in every route. What changes is what your components render to, which decides how much of your markup and CSS carries over. |
| 30 | + |
| 31 | +## Comparing the routes |
| 32 | + |
| 33 | +| | Renders to | Markup and styling | Native APIs | Pick it when | |
| 34 | +| --------------- | --------------------------------- | ------------------------------------- | ------------------------------------------------------ | ----------------------------------------------------- | |
| 35 | +| Capacitor | WebView | HTML and CSS, unchanged | JavaScript plugins backed by Swift and Java or Kotlin | You want to ship an existing web app with few changes | |
| 36 | +| Tauri | System WebView | HTML and CSS, unchanged | Rust commands, plus plugins in Rust, Swift, and Kotlin | You want a small binary and a Rust backend | |
| 37 | +| NativeScript | Native views | NativeScript elements instead of HTML | Direct access to platform APIs from JavaScript | You want native views without writing a renderer | |
| 38 | +| Custom renderer | Native views, or any non-DOM host | The host's own elements | Whatever the host provides | No existing integration covers your platform | |
| 39 | + |
| 40 | +## WebView shells |
| 41 | + |
| 42 | +A WebView is a browser engine embedded in a native app. |
| 43 | +The shell loads your Solid [SPA](/solid-router/rendering-modes/spa) into it, so components, routing, and CSS run as on the web, and plugins bridge to native features like the camera or push notifications. |
| 44 | + |
| 45 | +### Capacitor |
| 46 | + |
| 47 | +[Capacitor](https://capacitorjs.com/) wraps a web build in a native iOS/Android project and exposes native APIs to your Solid app through JavaScript plugins. |
| 48 | +The [`ionic-team/capacitor-solidjs-templates`](https://github.com/ionic-team/capacitor-solidjs-templates) repository provides official starter templates for Solid with Capacitor already configured. |
| 49 | + |
| 50 | +### Tauri |
| 51 | + |
| 52 | +[Tauri](https://v2.tauri.app/) uses the platform's system WebView instead of bundling one, for a smaller binary. |
| 53 | +Its [Solid frontend guide](https://v2.tauri.app/start/frontend/solidjs/) covers the setup, and Tauri 2 targets iOS and Android. |
| 54 | + |
| 55 | +## NativeScript |
| 56 | + |
| 57 | +[NativeScript](https://nativescript.org/) renders native views and exposes platform APIs to JavaScript. |
| 58 | +[`@nativescript-community/solid-js`](https://github.com/nativescript-community/solid-js) shims a DOM-like API over those views, so Solid's regular DOM renderer drives them without a WebView. |
| 59 | + |
| 60 | +Your reactive code carries over, but your markup does not: you write NativeScript elements such as `<stacklayout>` and `<label>`, not HTML. |
| 61 | + |
| 62 | +## Custom renderers with `solid-js/universal` |
| 63 | + |
| 64 | +Solid's default renderer, `solid-js/web`, assumes a DOM. |
| 65 | +A host without one, such as native views, needs its own way to create, update, and remove nodes, and `solid-js/universal` exposes `createRenderer` for that. |
| 66 | +It takes an object describing how to create, mutate, and traverse nodes on your host, and returns Solid's primitives (`render`, `effect`, `memo`, `insert`, and more) wired to it: |
| 67 | + |
| 68 | +```ts |
| 69 | +import { createRenderer } from "solid-js/universal"; |
| 70 | + |
| 71 | +const { |
| 72 | + render, |
| 73 | + effect, |
| 74 | + memo, |
| 75 | + createComponent, |
| 76 | + createElement, |
| 77 | + createTextNode, |
| 78 | + insertNode, |
| 79 | + insert, |
| 80 | + spread, |
| 81 | + setProp, |
| 82 | + mergeProps, |
| 83 | + use, |
| 84 | +} = createRenderer({ |
| 85 | + createElement(tag) { |
| 86 | + /* create a node for your host */ |
| 87 | + }, |
| 88 | + createTextNode(value) { |
| 89 | + /* create a text node for your host */ |
| 90 | + }, |
| 91 | + replaceText(textNode, value) { |
| 92 | + /* update a text node's value */ |
| 93 | + }, |
| 94 | + setProperty(node, name, value) { |
| 95 | + /* apply a prop/attribute to a node */ |
| 96 | + }, |
| 97 | + insertNode(parent, node, anchor) { |
| 98 | + /* attach node to parent, before anchor if given */ |
| 99 | + }, |
| 100 | + removeNode(parent, node) { |
| 101 | + /* detach node from parent */ |
| 102 | + }, |
| 103 | + isTextNode(node) { |
| 104 | + /* return whether node is a text node */ |
| 105 | + }, |
| 106 | + getParentNode(node) {}, |
| 107 | + getFirstChild(node) {}, |
| 108 | + getNextSibling(node) {}, |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +That lets Solid's fine-grained reactivity drive any tree-shaped host, not just the DOM. |
| 113 | +`createRenderer` only covers creating and updating nodes. Native views, a layout engine, and gesture handling are not included, so building on it directly is a project of its own. |
| 114 | + |
| 115 | +For the full `createRenderer` signature, see its [source on GitHub](https://github.com/solidjs/solid/blob/main/packages/solid/universal/src/index.ts). |
0 commit comments