solid README

项目的 README,来自 github.com/solidjs/solid。合并为一个 markdown 文件:README.md。

SolidJS

Build Status Coverage Status

NPM Version Discord Subreddit subscribers

Website • Docs • Tutorial • Playground • Discord

Solid is a declarative JavaScript library for building user interfaces. Instead of a Virtual DOM, it compiles templates to real DOM nodes and updates them with fine-grained reactivity. Declare your state and use it throughout your app — when a piece of state changes, only the code that depends on it re-runs.

At a glance

import { createSignal } from "solid-js";
import { render } from "@solidjs/web";

function Counter() {
  const [count, setCount] = createSignal(0);
  const doubled = () => count() * 2;

  // The component body runs once. Only `{doubled()}` updates when count changes.
  return <button onClick={() => setCount(c => c + 1)}>{doubled()}</button>;
}

render(() => <Counter />, document.getElementById("app")!);

Try it in our Playground.

Explain this!

Solid compiles your JSX to real DOM operations at build time. The component function runs once on mount; reactivity is per-expression, not per-render. The {doubled()} expression compiles to a tracked update that only touches that one DOM position when count() changes.

import { template, delegateEvents, insert } from "@solidjs/web";
import { createSignal } from "solid-js";

const _tmpl$ = /*#__PURE__*/ template(`<button>`);

function Counter() {
  const [count, setCount] = createSignal(0);
  const doubled = () => count() * 2;

  return (() => {
    const _el$ = _tmpl$();
    _el$._$$click = () => setCount(c => c + 1);
    insert(_el$, doubled);
    return _el$;
  })();
}
delegateEvents(["click"]);

Reads update on the next microtask (or call flush() for synchronous interop). Effects are split into a tracking compute phase and a side-effecting apply phase — see the docs for the full model.

Key features

Quick start

Start from a template:

npx degit solidjs/templates/solid-v2/basic my-app
cd my-app
npm install
npm run dev

Or add Solid to an existing Vite project:

npm i solid-js @solidjs/web
npm i -D vite @solidjs/vite-plugin
// vite.config.ts
import { defineConfig } from "vite";
import solid from "@solidjs/vite-plugin";

export default defineConfig({
  plugins: [solid()]
});

and point TypeScript at the web JSX types:

{
  "compilerOptions": {
    "jsx": "preserve",
    "jsxImportSource": "@solidjs/web"
  }
}

solid-js owns renderer-neutral component types, while renderer packages such as @solidjs/web and @solidjs/h own their JSX namespaces and jsx-runtime type entries.

Coming from Solid 1.x?

Read the migration guide. Solid 1.x is maintained on the 1.x branch, installs with npm i solid-js@v1-latest, and its docs are at v1.solidjs.com.

For LLMs / AI assistants

The public API differs from any Solid examples that predate 2.0. Before generating Solid 2.0 code, read:

The primer and the cheatsheet ship inside the solid-js npm package (node_modules/solid-js/skills/solid-2.0/SKILL.md, node_modules/solid-js/CHEATSHEET.md; the build also emits the cheatsheet as the solid-2.0-cheatsheet skill). The packages carry the tanstack-intent keyword, so skill registries that scan node_modules find them.

Repository

This is a PNPM workspaces monorepo. The core runtime (solid-js, @solidjs/signals, @solidjs/web, @solidjs/universal, @solidjs/diagnostics) always shares one version. The two compilers share a version when they release together, and @solidjs/h, @solidjs/html and @solidjs/element version on their own.

Package Path Purpose
solid-js packages/solid Core runtime — components, flow controls, context, hydration.
@solidjs/signals packages/signals Reactive primitives — signals, memos, effects, stores.
@solidjs/web packages/web Web platform runtime — render, hydrate, SSR, Portal, dynamic.
@solidjs/h packages/h Hyperscript / JSX factory.
@solidjs/html packages/html Build-less tagged template literals.
@solidjs/universal packages/universal Universal runtime for custom renderers.
@solidjs/element packages/element Web Components wrapper.
@solidjs/compiler packages/compiler Native Oxc JSX compiler.
@solidjs/babel-plugin packages/babel-plugin Babel plugin for JSX compilation.
@solidjs/diagnostics packages/diagnostics Dev-only diagnostics and attribution harness for tests and agents.

Runtime DOM operations live in @solidjs/web (and @solidjs/universal / @solidjs/h / @solidjs/html). The JSX compilers and the DOM runtime live in this repo, so file their issues here, not on DOM Expressions. The API reference in one page is packages/solid/CHEATSHEET.md, and the design notes are under documentation/solid-2.0/.

Why Solid?

Performant

Engineered for performance with years of research behind it — Solid's runtime cost is close to optimized vanilla JavaScript on the JS Framework Benchmark. Small, fully tree-shakable, fast on the server too. (Read more about Solid's performance.)

Powerful

Performant state management is built in (Context, Stores, Projections). Async is a first-class capability of computations, with <Loading> / <Errored> boundaries and isPending for revalidation indicators. Full SSR, streaming, and progressive hydration when you're ready to move to the server.

Pragmatic

Components are plain functions. Rendering is determined by how state is used — no rendering system to learn. Read-write segregation is encouraged but not enforced.

Productive

Built on JSX and TypeScript, integrates with the Vite ecosystem. Bare-metal abstractions give you direct access to the DOM. Growing ecosystem of primitives, component libraries, and build-time utilities.

More

Browser support

SolidJS Core supports the last 2 years of modern Firefox, Safari, Chrome, and Edge (desktop and mobile). IE and similar sunset browsers are not supported. For server environments, Node LTS, the latest Deno, and Cloudflare Worker runtimes are supported.

Community

Come chat on Discord. Solid's maintainers are active there, and we're always looking for contributions.

Contributors

Open Collective

Support us with a donation and help us continue our activities. [Contribute]

Sponsors

Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]

solid (solidjs/solid)

正在搜索数据库…

36.5K ·GitHub
这是什么?Solid 是一个用于构建用户界面的声明式 JavaScript 库。它不使用虚拟 DOM,而是把 JSX 模板编译成真实的 DOM 节点,并通过细粒度的响应式更新来修改它们。
它是完整的 UI 框架,还是只是一个渲染库?它的用法接近完整框架:组件就是普通函数,且只运行一次。Solid 在轻量的响应式核心之上,开箱即用地提供 Context、Stores、Suspense、流式 SSR、渐进式水合和错误边界。
如何新建一个 Solid 项目?运行 Solid 模板,用 Vite 搭建一个客户端渲染的应用(可选 JavaScript 或 TypeScript);如果你要把 Solid 接入已有环境,则可以在自己的 Babel、webpack 或 rollup 配置中添加 'babel-preset-solid'。
需要 Babel 或 Vite 这样的构建步骤吗?需要。Solid 的 JSX 必须先经过编译,所以你的工具链中需要 'babel-preset-solid'(或 Vite 插件)。如果使用 TypeScript,还必须设置 tsconfig 以支持 Solid 的 JSX。
Solid 能在 IE 这类旧浏览器中运行吗?不能。Solid 支持最近两年的现代浏览器(Firefox、Safari、Chrome、Edge),以及服务端的 Node LTS、Deno 和 Cloudflare Workers,但明确不支持 IE 及类似已停止支持的浏览器。
Solid 能在服务器端运行,而不只是客户端吗?可以。Solid 是同构且通用的:支持完整的服务端渲染、无服务器部署、流式渲染和渐进式水合,你甚至可以编写自定义渲染器,以适配其他运行环境。
Solid 与 React 的虚拟 DOM 方式有什么不同?Solid 直接把 JSX 编译为真实的 DOM 更新,并使用细粒度的响应式原语(createSignal),因此只有依赖已变更值的那部分代码会重新执行,而不是重新渲染整个组件并对虚拟 DOM 做差异比对。
像 React 那样,每次状态变化组件都会重新渲染吗?不会。组件只运行一次来搭建视图(即“render-once”模型);之后,只有与变化状态相关的特定响应式表达式会更新,而不是整个组件。
不用单独的库也能管理全局状态吗?可以。Solid 内置了 Context 和 Stores,用于高性能的状态管理,因此通常不需要第三方状态库。
和其他框架相比,Solid 快吗?Solid 体积小,支持 tree-shaking(摇树优化)。在 JS Framework Benchmark 上,无论是客户端渲染还是服务端渲染,它的表现都接近经过优化的原生 JavaScript。
有哪些类似的项目?gea, mitosis, vue, shaders, svelte, leptos, core, ice
作者
solidjs
大小
45.3 MB
星标
36.5K
时长
8年
提交 · 90 天