Why is this Sevlte useSelector implementation not calling the derived function?

29 Views Asked by At

I've been trying to get Redux working in Svelte to make porting some React based code that use useDispatch and useSelector hooks simpler.

I found a useful guide which leads to a Gist. I've gone through and implemented this approach (which I did manage to get a more primitive example working some months back) but now can't seem to get things going.

I've got a reproduction of my progress here https://svelte.dev/repl/9ae9be44ed814a2b857962aa191173ed?version=4.2.12 but essentially the bit I'm struggling with is the useSelector hook.

export function useSelector(selector, equalityFn) {
    if (!equalityFn) {
    equalityFn = (lhs, rhs) => lhs === rhs;
  }

    const store = getContext(STORE_KEY);
  let lastSelectorValue;

    return derived(store, ($state, set) => {
        console.log("This derived callback never fires");
        const selectorValue = selector($state);
        if (!equalityFn(selectorValue, lastSelectorValue)) {
            lastSelectorValue = selectorValue;
      set(lastSelectorValue);
        }
    });
}

For some reason when I set this up, the callback passed to derived never seems to get called.

Can anyone explain why this isn't working so I can fix the useSelector?

0

There are 0 best solutions below