New React Hooks

Planted on
Last tended on

Status: #seedling

Tags: #react

This is a note meant to be expanded as I come across new React hooks I haven't used before.

useSyncExternalStore

Docs: https://react.dev/reference/react/useSyncExternalStore

It lets you subscribe to an external store. It returns the snapshot of the data in the store.

const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot?)
  1. The subscribe function should subscribe to the store and return a function that unsubscribes.
  2. The getSnapshot function should read a snapshot of the data from the store.
Important

When possible, we recommend using built-in React state with useState and useReducer instead. The useSyncExternalStore API is mostly useful if you need to integrate with existing non-React code.

Sources