From 08c6d7a785ceb8b5725a46d764521df9f486e3f3 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 22 Oct 2024 15:15:57 -0700 Subject: [PATCH] Create twenty-boats-burn.md --- .changeset/twenty-boats-burn.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .changeset/twenty-boats-burn.md diff --git a/.changeset/twenty-boats-burn.md b/.changeset/twenty-boats-burn.md new file mode 100644 index 0000000000..d485e124bd --- /dev/null +++ b/.changeset/twenty-boats-burn.md @@ -0,0 +1,32 @@ +--- +"@latticexyz/stash": patch +--- + +Added `useStash` React hook. It's heavily inspired by Zustand's `useStore` and accepts a stash, a state selector, an an optional equality function to avoid unnecessary re-render cycles when returning unstable values. + +Also updated `getRecord` and `getRecords` to each take either a `stash` or `state` object for more ergonomic use with `useStash`. + +```ts +import { useStash } from "@latticexyz/stash/react"; +import { getRecord } from "@latticexyz/stash"; +import config from "../mud.config"; + +const tables = config.namespaces.app.tables; + +export function PlayerName({ playerId }) { + const record = useStash(stash, (state) => getRecord({ state, table: tables.Player, key: { playerId } })); + ... +} +``` + +```ts +import isEqual from "fast-deep-equal"; +import { useStash } from "@latticexyz/stash/react"; +import { getRecords } from "@latticexyz/stash"; +import config from "../mud.config"; + +export function PlayerNames() { + const record = useStash(stash, (state) => getRecords({ state, table: tables.Player }), { isEqual }); + ... +} +```