Skip to content
Myr

react.ts

This file

Path

src/react.ts

64 lines · 1.8 KiB · TypeScript

Last changed by

fix(react): useCallback for getSnapshot (#3192)Daishi Katobefore adoption5aa923e

History of this file

Take it

The walk stopped after 200 commits: lines older than f85898f point at it.

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 1–31why, one step back
1import React from 'react'
2import { createStore } from './vanilla.ts'3import type {4 ExtractState,5 Mutate,6 StateCreator,7 StoreApi,8 StoreMutatorIdentifier,9} from './vanilla.ts'1011type ReadonlyStoreApi<T> = Pick<12 StoreApi<T>,13 'getState' | 'getInitialState' | 'subscribe'14>1516const identity = <T>(arg: T): T => arg17export function useStore<S extends ReadonlyStoreApi<unknown>>(18 api: S,19): ExtractState<S>2021export function useStore<S extends ReadonlyStoreApi<unknown>, U>(22 api: S,23 selector: (state: ExtractState<S>) => U,24): U2526export function useStore<TState, StateSlice>(27 api: ReadonlyStoreApi<TState>,28 selector: (state: TState) => StateSlice = identity as any,29) {30 const slice = React.useSyncExternalStore(31 api.subscribe,

fix(react): useCallback for getSnapshot (#3192)Daishi Katobefore adoption5aa923e

lines 32–33why, one step back
32 React.useCallback(() => selector(api.getState()), [api, selector]),
33 React.useCallback(() => selector(api.getInitialState()), [api, selector]),

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 34–64why, one step back
34 )
35 React.useDebugValue(slice)36 return slice37}3839export type UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {40 (): ExtractState<S>41 <U>(selector: (state: ExtractState<S>) => U): U42} & S4344type Create = {45 <T, Mos extends [StoreMutatorIdentifier, unknown][] = []>(46 initializer: StateCreator<T, [], Mos>,47 ): UseBoundStore<Mutate<StoreApi<T>, Mos>>48 <T>(): <Mos extends [StoreMutatorIdentifier, unknown][] = []>(49 initializer: StateCreator<T, [], Mos>,50 ) => UseBoundStore<Mutate<StoreApi<T>, Mos>>51}5253const createImpl = <T>(createState: StateCreator<T, [], []>) => {54 const api = createStore(createState)5556 const useBoundStore: any = (selector?: any) => useStore(api, selector)5758 Object.assign(useBoundStore, api)5960 return useBoundStore61}6263export const create = (<T>(createState: StateCreator<T, [], []> | undefined) =>64 createState ? createImpl(createState) : createImpl) as Create