Solid is yet another declarative Javascript library for creating user interfaces. It does not use a Virtual DOM. Instead it opts to compile its templates down to real DOM nodes and wrap updates in fine grained computations. This way when your state updates only the code that depends on it runs.
- Real DOM with fine-grained updates (No Virtual DOM! No Dirty Checking Digest Loop!).
- Declarative data
- Simple composable primitives without the hidden rules.
- Function Components with no need for lifecycle methods or specialized configuration objects.
- Less than 10% slower vs optimized painfully imperative vanilla DOM code. See Solid on JS Framework Benchmark.
- Supports modern features like JSX Fragments, Context, Portals, Suspense, and Asynchronous Rendering.
- Webcomponent friendly
- Implicit event delegation with Shadow DOM Retargeting
- Shadow DOM Portals
- Custom Element friendly Suspense flow
A Simple Component looks like:
const HelloMessage = ({name}) => (
<div>
Hello {name}
</div>
);
render(
() => <HelloMessage name="Taylor" />,
document.getElementById("hello-example")
);
To use Solid with JSX (recommended) run:
> npm install solid-js babel-preset-solid
Or you can get started with a simple app with the CLI with by running:
> npm init solid app my-app
npm init solid <project-type> <project-name>
is available with npm 6+.
It all starts with State. State objects are immutable so to update you call their companion setter function. Through the use of proxies they give the control of an immutable interface and the performance of a mutable one. Note only Plain Objects and Arrays are deeply wrapped.
import { createState, onCleanup } from 'solid-js'
const CountingComponent = () => {
const [state, setState] = createState({counter: 0});
const interval = setInterval(() =>
setState({counter: state.counter + 1})
, 1000);
onCleanup(() => clearInterval(interval));
return <div>{(state.counter)}</div>;
}
You can also deep set:
const [state, setState] = createState({
user: {
firstName: 'John'
lastName: 'Smith'
}
});
setState('user', {firstName: 'Jake', middleName: 'Reese'});
You can also use functions:
const [state, setState] = createState({counter: 0});
setState('counter', c => c + 1);
This takes the form similar to ImmutableJS setIn for leaving all mutation control at the top level state object. Keep in mind that setState when setting an object attempts to merge instead of replace.
But where the magic happens is with computations(effects and memos) which automatically track dependencies.
createEffect(() =>
setState({
displayName: `${state.user.firstName} ${state.user.lastName}`
})
);
console.log(state.displayName); // Jake Smith
Whenever any dependency changes the State value will immediately update. JSX expressions can also be wrapped in effects so for something as trivial as a display name you could just inline the expression in the template and have it update automatically.
Solid State also exposes a reconcile method used with setState that does deep diffing to allow for automatic efficient interopt with immutable store technologies like Redux, Apollo, or RxJS.
const unsubscribe = store.subscribe(({ todos }) => (
setState(reconcile('todos', todos)));
);
onCleanup(() => unsubscribe());
Solid's rendering is done by the DOM Expressions library. This library provides a generic optimized runtime for fine grained libraries like Solid with the opportunity to use a number of different Rendering APIs. The best option is to use JSX pre-compilation with Babel Plugin JSX DOM Expressions to give the smallest code size, cleanest syntax, and most performant code. The compiler converts JSX to native DOM element instructions and wraps expressions to be wrapped in our computations when indicated by in inner parens {( )}
.
Prettier and some compile to JS libraries like CoffeeScript will strip Parenthesis causing issues with Solid's JSX. So unfortunately they are incompatible at this time.
The easiest way to get setup is add babel-preset-solid
to your .babelrc, or babel config for webpack, or rollup:
"presets": ["solid"]
Alternatively in non-compiled environments you can use Tagged Template Literals Lit DOM Expressions or even HyperScript with Hyper DOM Expressions.
For convenience Solid exports interfaces to runtimes for these as:
import h from 'solid-js/h';
import html from 'solid-js/html'
Remember you still need to install the library separately for these to work.
This project started as trying to find a small performant library to work with Web Components, that had easy interopt with existing standards. It is very much inspired by fine-grain change detection libraries like Knockout.js and RxJS. The idea here is to ease users into the world of Observable programming by keeping it transparent and starting simple. Classically the Virtual DOM as seen in React for all its advances has some signifigant trade-offs:
- The VDOM render while performant is still conceptually a constant re-render
- It feels much more imperative as variable declarations and iterative methods for constructing the tree are constantly re-evaluating.
- Reintroduced lifecycle function hell that breaks apart the declarative nature of the data. E.g., relying on blacklisting changes across the tree with shouldComponentUpdate.
- Homogenous promise of Components and the overly simplistic local state in practice:
- Imposes boundaries on components to solve performance concerns
- Places you into a very specific but not necessarily obvious structure
- Only served to make it more ambiguous when emerging best practices lead to specialized component classification anyway
- Abstracts debugging to the point a
<div />
is not longer just a div - VDOM libraries still are based around having specialized data objects.
So the driving questions here are:
- If the data is going to be specialized anyway can we use Proxies to move the complexity into it rather than the rendering while keeping the appearance simple?
- Can this free up existing constraints on how you modularize your view code?
- Does this approach ultimately provide more adaptibility while reducing the API surface?
- Is fine grained change detection fundamentally more performant than the Virtual DOM?
Admittedly it takes a strong reason to not go with the general consensus of best, and most supported libraries and frameworks. And React's Hooks API addresses the majority of what I once considered its most untenable faults. But I believe there is a better way out there than how we do it today.
I cover this in more detail in my Bring Your Own Framework Blog Series (links below).
- Counter Simple Counter
- Simple Todos Todos with LocalStorage persistence
- Simple Routing Use 'switch' control flow for simple routing
- Scoreboard Make use of hooks to do some simple transitions
- Async Resource Ajax requests to SWAPI with Promise cancellation
- Suspense Various Async loading with Solid's Suspend control flow
- Redux Undoable Todos Example from Redux site done with Solid.
- Simple Todos Template Literals Simple Todos using Lit DOM Expressions
- Simple Todos HyperScript Simple Todos using Hyper DOM Expressions
- TodoMVC Classic TodoMVC example
- Hacker News App Small application to showcase Solid and Solid Element
- WebComponent Todos Showing off Solid Element
- JS Framework Benchmark The one and only
- UIBench Benchmark a benchmark tests a variety of UI scenarios.
- DBMon Benchmark A benchmark testing ability of libraries to render unoptimized data.
- Sierpinski's Triangle Demo Solid implementation of the React Fiber demo.
- Solid Element Extensions to Solid.js that add a Web Component wrapper and Hot Module Replacement.
- DOM Expressions The renderer behind Solid.js that enables lightning fast fine grained performance.
- Babel Plugin JSX DOM Expressions Babel plugin that converts JSX to DOM Expressions.
- Lit DOM Expressions Tagged Template Literal API for DOM Expressions.
- Hyper DOM Expressions HyperScript API for DOM Expressions.
- React Solid State React Hooks API to use Solid.js paradigm in your existing React apps.
- Finding Fine Grained Reactive Programming Introduction to the inner workings of Solid's Reactive system.
- The Real Cost of UI Components Comparison of the cost of Components in different UI Libraries.
- The Fastest Way to Render the DOM Comparison of all Solid Renderers against the Fastest Libraries in the World.
- JavaScript UI Compilers: Comparing Svelte and Solid A closer look at precompiled UI libraries
- Building a Simple JavaScript App with Solid Dissecting building TodoMVC with Solid.
- Solid — The Best JavaScript UI Library You’ve Never Heard Of
- What Every JavaScript Framework Could Learn from React The lessons Solid learned from React.
- React Hooks: Has React Jumped the Shark? Comparison of React Hooks to Solid.
- How I wrote the Fastest JavaScript UI Framework The key to Solid's performance.
- Part 5: JS Frameworks in 2019
- Part 4: Rendering the DOM
- Part 3: Change Management in JavaScript Frameworks
- Part 2: Web Components as Containers
- Part 1: Writing a JS Framework in 2018
This project is still a work in progress. While Solid's change management is reaching stability (this repo), I am still refining the rendering APIs from the DOM Expressions.