Replies: 1 comment 3 replies
-
Ok, I worked around it like that. Still need to test it, but, yeah :) At least TS is happy :) import { useMemo } from "react";
import { createTrackedSelector } from "react-tracked";
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
K extends keyof StateType = keyof StateType
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: K
): StateType[K];
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
K extends keyof TSelectors = keyof TSelectors
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: K,
...args: Parameters<TSelectors[K]>
): ReturnType<TSelectors[K]>;
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {}
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: "state"
): StateType;
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
K extends keyof StateType = keyof StateType
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: K,
equalityFn?: TEqualityChecker<StateType[K]>
): StateType[K];
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
K extends keyof TSelectors = keyof TSelectors
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: K,
...args: [
...Parameters<TSelectors[K]>,
TEqualityChecker<ReturnType<TSelectors[K]>>?
]
): ReturnType<TSelectors[K]>;
export function useTrackedStoreValue<
StateType extends TState,
Mutators extends [StoreMutatorIdentifier, unknown][],
TActions extends Record<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
K extends keyof StateType | keyof TSelectors =
| keyof StateType
| keyof TSelectors
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
key: K,
...args: unknown[]
) {
const useTrackedState = useMemo(() => {
console.log("new tracked");
return createTrackedSelector(() =>
store.useValue(key as any, ...(args as any))
);
}, [store, key]);
return useTrackedState();
}
export const useTrackedThreadEntryWebResults = () => useTrackedStoreValue(useThreadEntryStore(), 'webResults'); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, if your selector returns an object, you get re-renders on any change to this object, even if you use only a subset of fields from it.
To get a tracked version of them, you need to use
react-tracked
directly.Also, having selectors available via getters like
store.mySelector
would be a killer, but I doubt that's possible :)But at least
useTracked(store, 'mySelector')
would be nice. Maybe it works already, but types are wrong returning a union type of all the possible value types ofTState[Key]
Currently, I have to work around it this way, which is pretty ugly (I'm unsure if it even works; I haven't launched the app yet :D)
Beta Was this translation helpful? Give feedback.
All reactions