Website • API Docs • Features Tutorial • Playground • Discord
Solid is a declarative JavaScript library for creating user interfaces. Instead of using a Virtual DOM, it compiles its templates to real DOM nodes and updates them with fine-grained reactions. Declare your state and use it throughout your app, and when a piece of state changes, only the code that depends on it will rerun.
At a Glance
import { createSignal } from "solid-js"; import { render } from "solid-js/web"; function Counter() { const [count, setCount] = createSignal(0); const doubleCount = () => count() * 2; console.log("The body of the function runs once..."); return ( <> <button onClick={() => setCount(c => c + 1)}> {doubleCount()} </button> </> ); } render(Counter, document.getElementById("app")!);
Try this code in our playground!
Explain this!import { createSignal } from "solid-js"; import { render } from "solid-js/web"; // A component is just a function that returns a DOM node function Counter() { // Create a piece of reactive state, giving us an accessor, count(), and a setter, setCount() const [count, setCount] = createSignal(0); //To create derived state, just wrap an expression in a function const doubleCount = () => count() * 2; console.log("The body of the function runs once..."); // JSX allows you to write HTML within your JavaScript function and include dynamic expressions using the { } syntax // The only part of this that will ever rerender is the doubleCount() text. return ( <> <button onClick={() => setCount(c => c + 1)}> Increment: {doubleCount()} </button> </> ); } // The render function mounts a component onto your page render(Counter, document.getElementById("app")!);
Solid compiles your JSX down to efficient real DOM updates. It uses the same reactive primitives (createSignal) at runtime but making sure there's as little rerendering as possible. Here's what that looks like in this example:
"import { template as _$template } from "solid-js/web";
import { delegateEvents as _$delegateEvents } from "solid-js/web";
import { insert as _$insert } from "solid-js/web";
//The compiler pulls out any static HTML
const _tmpl$ = /*#__PURE__*/_$template(`