react-fine-grained-signals 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +62 -2
- package/README.md +77 -2
- package/package.json +1 -1
package/README.ja.md
CHANGED
|
@@ -13,13 +13,73 @@
|
|
|
13
13
|
## インストール
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
# 公開予定のパッケージ名です。まだnpmには公開していません。
|
|
17
16
|
pnpm add react-fine-grained-signals alien-signals
|
|
18
17
|
```
|
|
19
18
|
|
|
20
19
|
`alien-signals` はpeer dependencyなので、上のように併せてinstallしてください。
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
## セットアップ
|
|
22
|
+
|
|
23
|
+
プリミティブとフックは、installした時点で動作します。buildツールやコンパイラの設定は不要です。
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { useSignal, useSignals } from "react-fine-grained-signals";
|
|
27
|
+
|
|
28
|
+
function Counter() {
|
|
29
|
+
useSignals();
|
|
30
|
+
const count = useSignal(0);
|
|
31
|
+
|
|
32
|
+
return <button onClick={() => count.value++}>{count.value}</button>;
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
以下の2つの最適化は任意であり、互いに独立しています。どちらもinstall後に別途設定するもので、上記のフックを使うだけなら不要です。
|
|
37
|
+
|
|
38
|
+
### JSXランタイム — DOMへの直接バインディング
|
|
39
|
+
|
|
40
|
+
ネイティブホスト要素の子要素として使われたsignalが、周囲のコンポーネントを再レンダーせずに、そのDOMノードだけを更新できるようになります。
|
|
41
|
+
|
|
42
|
+
ファイル単位で有効にするには、先頭行にpragmaを書きます。他のファイルはReactのJSXランタイムのままになるため、アプリの一部だけで直接バインディングを使いたい場合はこちらを選んでください。[`examples/react-router`](examples/react-router)がこの構成です。
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
/** @jsxImportSource react-fine-grained-signals */
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
プロジェクト全体で有効にする場合は `tsconfig.json` に設定します。
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"compilerOptions": {
|
|
53
|
+
"jsx": "react-jsx",
|
|
54
|
+
"jsxImportSource": "react-fine-grained-signals"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Viteはこの2つを `tsconfig.json` から読むため、`vite.config.ts` 側にJSXの設定は不要です。既定の変換でも `@vitejs/plugin-react` を使う場合でも同じです。
|
|
60
|
+
|
|
61
|
+
このランタイムが対象とするのはネイティブ要素と、意図的に絞ったpropsだけです。Reactコンポーネントのpropsや子要素に渡したsignalはアンラップされません。許可リスト全体と、JSXをBabelで変換するツールチェーン(`tsconfig.json` を読みません)については[JSXのsignal子要素とhost binding](docs/jsx-bindings.ja.md)を参照してください。
|
|
62
|
+
|
|
63
|
+
### ビルドplugin — `useSignals()` の自動挿入
|
|
64
|
+
|
|
65
|
+
追跡境界をbuild時に挿入するため、コンポーネント側で `useSignals()` を手書きする必要がなくなります。別packageなので個別にinstallします。
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
pnpm add -D unplugin-react-fine-grained-signals
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
そのうえで、bundlerに応じたentry point(`/vite`、`/rollup`、`/webpack`、`/rspack`、`/esbuild`)をbuild設定に追加します。
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
// vite.config.ts
|
|
75
|
+
import signals from "unplugin-react-fine-grained-signals/vite";
|
|
76
|
+
|
|
77
|
+
export default defineConfig({
|
|
78
|
+
plugins: [signals({ mode: "auto" })],
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
オプションの詳細と、フックを手書きする場合との使い分けについては[描画最適化](docs/rendering-optimization.ja.md)を参照してください。
|
|
23
83
|
|
|
24
84
|
## 開発
|
|
25
85
|
|
package/README.md
CHANGED
|
@@ -13,13 +13,88 @@ See [`docs/README.md`](docs/README.md) for guides on using the library, design i
|
|
|
13
13
|
## Installation
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
# Planned package name — it is not published to npm yet.
|
|
17
16
|
pnpm add react-fine-grained-signals alien-signals
|
|
18
17
|
```
|
|
19
18
|
|
|
20
19
|
`alien-signals` is a peer dependency, so install it alongside this package.
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
## Setup
|
|
22
|
+
|
|
23
|
+
The primitives and hooks work as soon as the package is installed — no build
|
|
24
|
+
or compiler configuration is involved:
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { useSignal, useSignals } from "react-fine-grained-signals";
|
|
28
|
+
|
|
29
|
+
function Counter() {
|
|
30
|
+
useSignals();
|
|
31
|
+
const count = useSignal(0);
|
|
32
|
+
|
|
33
|
+
return <button onClick={() => count.value++}>{count.value}</button>;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The two optimizations below are opt-in and independent of each other. Both are
|
|
38
|
+
configured after installing, and neither is needed to use the hooks above.
|
|
39
|
+
|
|
40
|
+
### JSX runtime — direct DOM bindings
|
|
41
|
+
|
|
42
|
+
Lets a signal used as the child of a native host element update that DOM node
|
|
43
|
+
on its own, without re-rendering the component around it.
|
|
44
|
+
|
|
45
|
+
Opt in one file at a time with a pragma on the first line. Every other file
|
|
46
|
+
keeps React's JSX runtime, so this is the form to reach for when only part of
|
|
47
|
+
an app needs the direct bindings — it is how
|
|
48
|
+
[`examples/react-router`](examples/react-router) is set up:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
/** @jsxImportSource react-fine-grained-signals */
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Or opt the whole project in through `tsconfig.json`:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"compilerOptions": {
|
|
59
|
+
"jsx": "react-jsx",
|
|
60
|
+
"jsxImportSource": "react-fine-grained-signals"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Vite reads both of those from `tsconfig.json` — with its default transform and
|
|
66
|
+
with `@vitejs/plugin-react` alike — so `vite.config.ts` needs no JSX
|
|
67
|
+
configuration of its own.
|
|
68
|
+
|
|
69
|
+
The runtime covers native elements only, and a deliberately narrow set of
|
|
70
|
+
props. Signals passed to a React component's props or children are not
|
|
71
|
+
unwrapped. See [JSX signal children and host bindings](docs/jsx-bindings.md)
|
|
72
|
+
for the full allow-list, and for toolchains that transform JSX through Babel,
|
|
73
|
+
which do not read `tsconfig.json`.
|
|
74
|
+
|
|
75
|
+
### Build plugin — automatic `useSignals()`
|
|
76
|
+
|
|
77
|
+
Inserts the tracking boundary during the build, so components no longer call
|
|
78
|
+
`useSignals()` by hand. It is a separate package:
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
pnpm add -D unplugin-react-fine-grained-signals
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
It then needs a bundler entry point — `/vite`, `/rollup`, `/webpack`,
|
|
85
|
+
`/rspack`, or `/esbuild` — added to the build configuration:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// vite.config.ts
|
|
89
|
+
import signals from "unplugin-react-fine-grained-signals/vite";
|
|
90
|
+
|
|
91
|
+
export default defineConfig({
|
|
92
|
+
plugins: [signals({ mode: "auto" })],
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
See [Rendering optimization](docs/rendering-optimization.md) for the options
|
|
97
|
+
and for the trade-offs against calling the hook by hand.
|
|
23
98
|
|
|
24
99
|
## Development
|
|
25
100
|
|