命名狀態

使用相同名稱調用時將返回相同的單例 store。


基於組件內部調用設計的命名模式

命名模式可以在 React 組件內部方便使用。使用相同名稱和相同存儲選項將重用相同的條目和返回對象。依賴於先前值的更新,如數字增加,應該以 setCount((prev) => prev + 1) 的形式編寫。

tsx
import { createRxState } from "@byeolnaerim/global-rx-state";

function A_Component() {
  const { setCountInner, useCountInner } = createRxState(0, "countInner", { storage: "auto" });
  const countInner = useCountInner();

  return (
    <button onClick={() => setCountInner((prev) => prev + 1)}>
      {countInner}
    </button>
  );
}

function B_Component() {
  const { useCountInner } = createRxState(0, "countInner", { storage: "auto" });
  const countInner = useCountInner();

  return <span>현재 count: {countInner}</span>;
}
© 2026 Byeolnaerim. 版權所有。介紹隱私政策