CodeSampleX

示例

react 18.3.1: useSyncExternalStore

已验证示例 — npm react 18.3.1: useSyncExternalStore. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: react package.json declares export mappings and…

sha256:15d7e0a7ec22548a0bb948800607eec3463406efc7d903c0f14c4cd4466450e7

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

案例

HOW
目标
verify useSyncExternalStore in pkg:npm/react@18.3.1
包
符号
  • useSyncExternalStore
环境
node 22.23.2
创建时间
2026-08-27T10:08:16Z

契约

  1. react package.json declares export mappings and version 18.3.1
  2. react exports useSyncExternalStore as a function on the default client runtime interface
  3. react useSyncExternalStore throws a TypeError when invoked outside an active component render dispatcher
  4. react useSyncExternalStore delegates subscribe, getSnapshot, and getServerSnapshot callbacks to the active ReactCurrentDispatcher
  5. react useSyncExternalStore passes undefined for omitted getServerSnapshot argument to the dispatcher

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify useSyncExternalStore in pkg:npm/react@18.3.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/react@18.3.1
Demonstrate these symbols/APIs:
  - useSyncExternalStore

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:99643031054d76c1636efb4eb9bcc5e73fc9efb6e866d994c9e7962de4dd8b36","contract":["react package.json declares export mappings and version 18.3.1","react exports useSyncExternalStore as a function on the default client runtime interface","react useSyncExternalStore throws a TypeError when invoked outside an active component render dispatcher","react useSyncExternalStore delegates subscribe, getSnapshot, and getServerSnapshot callbacks to the active ReactCurrentDispatcher","react useSyncExternalStore passes undefined for omitted getServerSnapshot argument to the dispatcher"],"goal":"verify useSyncExternalStore in pkg:npm/react@18.3.1","kind":"HOW","packages":["pkg:npm/react@18.3.1"],"schemaVersion":1,"symbols":["useSyncExternalStore"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/react@18.3.1"],"schemaVersion":1,"subject":"pkg:npm/react@18.3.1","symbols":["useSyncExternalStore"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-react-use-sync-external-store",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-react-use-sync-external-store",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "react": "18.3.1"
      }
    },
    "node_modules/js-tokens": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
      "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
      "license": "MIT"
    },
    "node_modules/loose-envify": {
      "version": "1.4.0",
      "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",
      "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==",
      "license": "MIT",
      "dependencies": {
        "js-tokens": "^3.0.0 || ^4.0.0"
      },
      "bin": {
        "loose-envify": "cli.js"
      }
    },
    "node_modules/react": {
      "version": "18.3.1",
      "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz",
      "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==",
      "license": "MIT",
      "dependencies": {
        "loose-envify": "^1.1.0"
      },
      "engines": {
        "node": ">=0.10.0"
      }
    }
  }
}
package.json
{
  "name": "sample-react-use-sync-external-store",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "dependencies": {
    "react": "18.3.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify useSyncExternalStore in pkg:npm/react@18.3.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/react@18.3.1"
  ],
  "symbols": [
    "useSyncExternalStore"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import React from 'react';

const require = createRequire(import.meta.url);

// Contract 1: react package.json declares export mappings and version 18.3.1
{
  const pkgJsonPath = require.resolve('react/package.json');
  assert.ok(fs.existsSync(pkgJsonPath));
  const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
  assert.strictEqual(pkgJson.name, 'react');
  assert.strictEqual(pkgJson.version, '18.3.1');
  assert.strictEqual(pkgJson.exports['.']['default'], './index.js');
}

// Contract 2: react exports useSyncExternalStore as a function on the default client runtime interface
{
  assert.strictEqual(typeof React.useSyncExternalStore, 'function');
  assert.strictEqual(React.version, '18.3.1');
}

// Contract 3: react useSyncExternalStore throws a TypeError when invoked outside an active component render dispatcher
{
  const internals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
  assert.ok(internals && typeof internals === 'object');
  assert.strictEqual(internals.ReactCurrentDispatcher.current, null);

  assert.throws(
    () => {
      React.useSyncExternalStore(() => () => {}, () => 0);
    },
    {
      name: 'TypeError'
    }
  );
}

// Contract 4: react useSyncExternalStore delegates subscribe, getSnapshot, and getServerSnapshot callbacks to the active ReactCurrentDispatcher
{
  const internals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
  let receivedCall = null;
  const mockSnapshot = { count: 42 };

  internals.ReactCurrentDispatcher.current = {
    useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) {
      receivedCall = { subscribe, getSnapshot, getServerSnapshot };
      return mockSnapshot;
    }
  };

  try {
    const subscribe = () => () => {};
    const getSnapshot = () => mockSnapshot;
    const getServerSnapshot = () => ({ count: 0 });

    const result = React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);

    assert.strictEqual(result, mockSnapshot);
    assert.ok(receivedCall !== null);
    assert.strictEqual(receivedCall.subscribe, subscribe);
    assert.strictEqual(receivedCall.getSnapshot, getSnapshot);
    assert.strictEqual(receivedCall.getServerSnapshot, getServerSnapshot);
  } finally {
    internals.ReactCurrentDispatcher.current = null;
  }
}

// Contract 5: react useSyncExternalStore passes undefined for omitted getServerSnapshot argument to the dispatcher
{
  const internals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
  let receivedCall = null;

  internals.ReactCurrentDispatcher.current = {
    useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) {
      receivedCall = { subscribe, getSnapshot, getServerSnapshot };
      return 'client-value';
    }
  };

  try {
    const subscribe = () => () => {};
    const getSnapshot = () => 'client-value';

    const result = React.useSyncExternalStore(subscribe, getSnapshot);

    assert.strictEqual(result, 'client-value');
    assert.ok(receivedCall !== null);
    assert.strictEqual(receivedCall.subscribe, subscribe);
    assert.strictEqual(receivedCall.getSnapshot, getSnapshot);
    assert.strictEqual(receivedCall.getServerSnapshot, undefined);
  } finally {
    internals.ReactCurrentDispatcher.current = null;
  }
}

console.log('All react.useSyncExternalStore contract tests passed.');

原始种子者

匿名