SolidJS
SolidJS is a declarative JavaScript library for building user interfaces, currently stable at version 1.9.12, with a 2.0 beta release recently announced. It distinguishes itself from other UI libraries by compiling JSX templates directly to real DOM operations and fine-grained reactive primitives, avoiding a virtual DOM. This approach typically leads to excellent performance characteristics and a smaller bundle size. Solid focuses on explicit reactivity through signals, memos, and effects, providing granular control over updates without component re-renders. The project maintains a regular release cadence, primarily focusing on quality-of-life improvements and performance optimizations within the 1.x line, while actively developing the significant architectural changes and API re-evaluations slated for the upcoming 2.0 release.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'call') or (intermediate value) is not a function
cause Attempting to use a SolidJS signal (or other reactive primitive) as a direct value without calling it as a function.fixAlways invoke signals as functions to retrieve their current value, e.g., `count()` instead of `count`. -
Error: Hydration completed but element was not found.
cause Mismatch between the server-rendered HTML and the client-side SolidJS application's initial render output during hydration. Often caused by conditional rendering differences or external scripts modifying the DOM.fixEnsure identical rendering logic on both server and client. Avoid direct DOM manipulation by third-party scripts before hydration completes. Use Solid's built-in control flow components (`<Show>`, `<For>`) for conditional rendering. -
ReferenceError: createSignal is not defined
cause Missing or incorrect import statement for a SolidJS reactive primitive or utility.fixAdd the correct named import from `'solid-js'` or `'solid-js/web'`, e.g., `import { createSignal } from 'solid-js';`. -
Uncaught SyntaxError: Cannot use import statement outside a module (in browser console)
cause Attempting to run ES module syntax (`import`/`export`) directly in a browser without a build step, or without `type="module"` on the script tag.fixConfigure a build tool (e.g., Vite, Webpack, Rollup) with `vite-plugin-solid` or `babel-preset-solid` to correctly transpile and bundle your SolidJS application for browser execution. Ensure your `package.json` specifies `type: "module"` or use a bundler.
Warnings
- breaking The announcement of Solid 2.0 Beta (v2.0.0-beta.0) signifies an upcoming major version with likely significant breaking changes, API re-evaluations, and a focus on migration strategies. Developers should review the roadmap and beta releases for changes.
- deprecated Starting with v1.7.0, Solid began a migration roadmap to v2.0, introducing new APIs while reasonably deprecating existing ones. Developers should monitor release notes for specific deprecation warnings and future removal plans.
- gotcha Features like streaming HTML, isomorphic error boundaries, multiple async hydration roots, partial hydration, and islands (introduced in v1.3.0 and v1.6.0) can be challenging to configure correctly, often leading to hydration mismatches or performance issues if not handled precisely.
- breaking A long-standing issue with proper merging of JSX spreads on native elements was addressed in v1.6.0. This fix might alter behavior for applications that previously relied on or implicitly handled the buggy spread merging, potentially requiring adjustments.
- gotcha The `createUniqueId` utility (introduced in v1.1.0), while universal, only guarantees unique IDs on the server when used within hydratable components. Its behavior in non-hydratable server contexts might not meet expectations for uniqueness across multiple server renders.
Install
-
npm install solid-js -
yarn add solid-js -
pnpm add solid-js
Imports
- createSignal
const createSignal = require('solid-js').createSignal;import { createSignal } from 'solid-js'; - render
import { render } from 'solid-js'; // or const render = require('solid-js/web').render;import { render } from 'solid-js/web'; - For
import For from 'solid-js';
import { For } from 'solid-js';
Quickstart
import { createSignal, createEffect, onCleanup } from 'solid-js';
import { render } from 'solid-js/web';
function Counter() {
const [count, setCount] = createSignal(0);
createEffect(() => {
console.log(`Current count: ${count()}`);
// Example of cleanup logic for an effect
onCleanup(() => console.log('Cleaning up effect for count change.'));
});
return (
<div>
<h1>SolidJS Counter</h1>
<p>Count: {count()}</p>
<button onClick={() => setCount(c => c + 1)}>Increment</button>
<button onClick={() => setCount(c => c - 1)}>Decrement</button>
</div>
);
}
const appRoot = document.getElementById('app');
if (appRoot) {
render(() => <Counter />, appRoot);
} else {
console.error("Element with id 'app' not found.");
}