首页  >  文章  >  web前端  >  Zustand源码中的createWithEqualityFnImpl有解释。

Zustand源码中的createWithEqualityFnImpl有解释。

DDD
DDD原创
2024-09-13 06:25:02733浏览

在本文中,我们将通过记录其一些值来分析 createWithEqualityFnImpl 是如何实现的,以便更好地理解。

createWithEqualityFnImpl in Zustand’s source code explained.

从上图可以看出,createWithEqualityFn 调用了函数 createWithEqualityFnImpl。此模式在 vanilla.ts 中使用,如下所示:

export const createStore = ((createState) =>
  createState ? createStoreImpl(createState) : createStoreImpl) as CreateStore

createStore 调用 createStoreImpl,createWithEqualityFn 调用 createWithhEqualityFnImpl。

在执行createWithEqualityFn之前,我们先了解一下使用createWithEqualityFn的目的。

createWithEqualityFnImpl in Zustand’s source code explained.

以上截图来自https://github.com/pmndrs/zustand/tree/main

您可以使用 createWithEqualityFn 来更好地控制重新渲染。

createWithEqualityFn

const createWithEqualityFnImpl = <T>(
  createState: StateCreator<T, [], []>,
  defaultEqualityFn?: <U>(a: U, b: U) => boolean,
) => {
  const api = createStore(createState)

  const useBoundStoreWithEqualityFn: any = (
    selector?: any,
    equalityFn = defaultEqualityFn,
  ) => useStoreWithEqualityFn(api, selector, equalityFn)

  Object.assign(useBoundStoreWithEqualityFn, api)

  return useBoundStoreWithEqualityFn
}

useStoreWithEqualityFn

export function useStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>>(
  api: S,
): ExtractState<S>

export function useStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>, U>(
  api: S,
  selector: (state: ExtractState<S>) => U,
  equalityFn?: (a: U, b: U) => boolean,
): U

export function useStoreWithEqualityFn<TState, StateSlice>(
  api: ReadonlyStoreApi<TState>,
  selector: (state: TState) => StateSlice = identity as any,
  equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
  const slice = useSyncExternalStoreWithSelector(
    api.subscribe,
    api.getState,
    api.getInitialState,
    selector,
    equalityFn,
  )
  useDebugValue(slice)
  return slice
}

这是一个重载函数。 useStoreWithEqualityFn 有 3 个定义:

export function useStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>>(
  api: S,
): ExtractState<S>

export function useStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>, U>(
  api: S,
  selector: (state: ExtractState<S>) => U,
  equalityFn?: (a: U, b: U) => boolean,
): U

export function useStoreWithEqualityFn<TState, StateSlice>(
  api: ReadonlyStoreApi<TState>,
  selector: (state: TState) => StateSlice = identity as any,
  equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {

createWithEqualityFnImpl in Zustand’s source code explained.

让我们看看切片值是什么样的:

createWithEqualityFnImpl in Zustand’s source code explained.

value: 1 被跳过,因为我们设置了代码以避免值为 1 时重新渲染,如下所示:

createWithEqualityFnImpl in Zustand’s source code explained.

关于我们:

在 Think Throo,我们的使命是教授受开源项目启发的最佳实践。

通过在 Next.js/React 中练习高级架构概念,将您的编码技能提高 10 倍,学习最佳实践并构建生产级项目。

我们是开源的 — https://github.com/thinkthroo/thinkthroo (请给我们一颗星!)

通过我们基于代码库架构的高级课程来提高您的团队的技能。请通过 hello@thinkthroo.com 联系我们以了解更多信息!

参考资料:

  1. https://github.com/pmndrs/zustand/blob/main/src/traditional.ts#L74

  2. https://github.com/pmndrs/zustand/blob/main/src/traditional.ts#L39



以上是Zustand源码中的createWithEqualityFnImpl有解释。的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn