solid README

The project's README from github.com/solidjs/solid. As one markdown file: README.md.

SolidJS

Build Status Coverage Status

NPM Version Discord Subreddit subscribers

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(`<button>Increment: `);

import { createSignal, createEffect } 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 (() => {
    //_el$ is a real DOM node!
    const _el$ = _tmpl$();
    _el$.$$click = () => setCount(c => c + 1);
     //This inserts the count as a child of the button in a way that allows count to update without rerendering the whole button
    _$insert(_el$, doubleCount);
    return _el$;
  })();
}
render(Counter, document.getElementById("app"));
_$delegateEvents(["click"]);

Key Features

Quick Start

You can get started with a simple app by running the following in your terminal:

> npx degit solidjs/templates/js my-app
> cd my-app
> npm i # or yarn or pnpm
> npm run dev # or yarn or pnpm

Or for TypeScript:

> npx degit solidjs/templates/ts my-app
> cd my-app
> npm i # or yarn or pnpm
> npm run dev # or yarn or pnpm

This will create a minimal, client-rendered application powered by Vite.

Or you can install the dependencies in your own setup. To use Solid with JSX (recommended), run:

> npm i -D babel-preset-solid
> npm i solid-js

The easiest way to get set up is to add babel-preset-solid to your .babelrc, babel config for webpack, or rollup configuration:

"presets": ["solid"]

For TypeScript to work, remember to set your .tsconfig to handle Solid's JSX:

"compilerOptions": {
  "jsx": "preserve",
  "jsxImportSource": "solid-js",
}

Why Solid?

Performant

Meticulously engineered for performance and with half a decade of research behind it, Solid's performance is almost indistinguishable from optimized vanilla JavaScript (See Solid on the JS Framework Benchmark). Solid is small and completely tree-shakable, and fast when rendering on the server, too. Whether you're writing a fully client-rendered SPA or a server-rendered app, your users see it faster than ever. (Read more about Solid's performance from the library's creator.)

Powerful

Solid is fully-featured with everything you can expect from a modern framework. Performant state management is built-in with Context and Stores: you don't have to reach for a third party library to manage global state (if you don't want to). With Resources, you can use data loaded from the server like any other piece of state and build a responsive UI for it thanks to Suspense and concurrent rendering. And when you're ready to move to the server, Solid has full SSR and serverless support, with streaming and progressive hydration to get to interactive as quickly as possible. (Check out our full interactive features walkthrough.)

Pragmatic

Do more with less: use simple, composable primitives without hidden rules and gotchas. In Solid, components are just functions - rendering is determined purely by how your state is used - so you're free to organize your code how you like and you don't have to learn a new rendering system. Solid encourages patterns like declarative code and read-write segregation that help keep your project maintainable, but isn't opinionated enough to get in your way.

Productive

Solid is built on established tools like JSX and TypeScript and integrates with the Vite ecosystem. Solid's bare-metal, minimal abstractions give you direct access to the DOM, making it easy to use your favorite native JavaScript libraries like D3. And the Solid ecosystem is growing fast, with custom primitives, component libraries, and build-time utilities that let you write Solid code in new ways.

More

Check out our official documentation or browse some examples

Browser Support

SolidJS Core is committed to supporting the last 2 years of modern browsers including Firefox, Safari, Chrome and Edge (for desktop and mobile devices). We do not support IE or similar sunset browsers. For server environments, we support Node LTS and the latest Deno and Cloudflare Worker runtimes.

Testing Powered By SauceLabs

Community

Come chat with us on Discord! Solid's creator and the rest of the core team 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)

Searching Database…

36.1K ·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。
What are similar projects?mitosis, vue, shaders, svelte, leptos, framework, core, ice
Creator
solidjs
Size
39.6 MB
Stars
36.1K
Age
8y
Commits · 90 Days