Skip to content

Commit

Permalink
Create twenty-boats-burn.md
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 22, 2024
1 parent 6bbcebc commit 08c6d7a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .changeset/twenty-boats-burn.md
Original file line number Diff line number Diff line change
@@ -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 });
...
}
```

0 comments on commit 08c6d7a

Please sign in to comment.