Skip to content

react-tracked and Zustand v5, no longer working for my use case #3072

Closed Answered by zeorin
zeorin asked this question in Q&A
Discussion options

You must be logged in to vote

I fixed it.

TL;DR: instead of applying the tracking proxy to the value returned by the selector, I apply it to the value returned by useStoreWithEqualityFn, and use proxy-compare's isChanged as part of the equality function.

import { useCallback, useMemo } from 'react';

import { createProxy, isChanged } from 'proxy-compare';
import { useStoreWithEqualityFn } from 'zustand/traditional';
import type { StoreApi } from 'zustand/vanilla';

type ReadonlyStoreApi<T> = Pick<
  StoreApi<T>,
  'getState' | 'getInitialState' | 'subscribe'
>;

function identity<T>(x: T): T;
function identity(): undefined;
function identity(x?: unknown) {
  return x;
}

export const useTrackedStore = <const State, const

Replies: 3 comments 9 replies

Comment options

You must be logged in to vote
5 replies
@zeorin
Comment options

@zeorin
Comment options

@dai-shi
Comment options

@zeorin
Comment options

@dai-shi
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@zeorin
Comment options

@dai-shi
Comment options

@mooalot
Comment options

@dai-shi
Comment options

Answer selected by zeorin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants