qwik icon indicating copy to clipboard operation
qwik copied to clipboard

[🐞] `useStore` reactivity is broken if key isn't initialized in store object

Open jessezhang91 opened this issue 2 years ago • 5 comments

Which component is affected?

Qwik Runtime

Describe the bug

I am updating a store with an optional key. What I expect is downstream reactivity to occur based on the store's change. What actually is happening is no changes occur.

Reproduction

https://stackblitz.com/edit/qwik-starter-dgqpxj?file=src%2Froutes%2Findex.tsx

Steps to reproduce

Copied from reproduction link above:

  const signalWithInit = useSignal('init');
  const signalWithoutInit = useSignal<string | undefined>();

  const storeWithInit = useStore<{ value: string }>({ value: 'init' });
  const storeWithoutInit = useStore<{ value?: string }>({});

  useVisibleTask$(
    () => {
      signalWithInit.value = 'visible-task';
      signalWithoutInit.value = 'visible-task';

      storeWithInit.value = 'visible-task';
      storeWithoutInit.value = 'visible-task';
    },
    { strategy: 'document-ready' }
  );

storeWithoutInit.value will not trigger any downstream changes (e.g. rendering content or triggering tracks).

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 931.25 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.13.0 - ~/.nvm/versions/node/v18.13.0/bin/node
    npm: 8.19.3 - ~/.nvm/versions/node/v18.13.0/bin/npm
  Browsers:
    Chrome: 113.0.5672.92
    Firefox Developer Edition: 113.0
    Safari: 16.4
  npmPackages:
    @builder.io/qwik: ^1.1.1 => 1.1.1 
    @builder.io/qwik-city: ^1.1.1 => 1.1.1 
    undici: ^5.22.1 => 5.22.1 
    vite: 4.3.5 => 4.3.5

Additional Information

Here's the same repro code as above but in v1.0.0: https://stackblitz.com/edit/qwik-starter-odovoh?file=src%2Froutes%2Findex.tsx

It does render/function as expected there.

The workaround I'm going to attempt is to not have any optional keys in my store types and instead have it defaulted to undefined instead.

jessezhang91 avatar May 11 '23 17:05 jessezhang91

This is a regression between 1.0.0 and 1.1.0

1.0.0 Playground

1.1.0 Playground

cmbartschat avatar May 12 '23 16:05 cmbartschat

Highest priority to fix! also, add a new regression test. Working on it!

manucorporat avatar May 12 '23 17:05 manucorporat

We got a fix!

manucorporat avatar May 12 '23 17:05 manucorporat

Short question, since it's about broken reactivity and it was introduced with v1.1.0, could it fix the context reactivity problem too? https://discord.com/channels/842438759945601056/1106601727458824302/1106601727458824302

Zeiver avatar May 12 '23 18:05 Zeiver

@Zeiver the discord thread does not have much source code i can use to reproduce. As a rule thumb, if you want a bug to be fixed, github is the place!! I pay x50 times more attention to github issues than conversations in Discord.

The reason is because i need the issue reporter to fill the issue template correctly, it really saves time

manucorporat avatar May 12 '23 18:05 manucorporat

Okay gonna try to create an example with Stackblitz.

Zeiver avatar May 12 '23 18:05 Zeiver

https://stackblitz.com/edit/node-xenwez?file=src/components/starter/example/example.tsx So I only created a 1.1.0 version, but it's the same problem. If I give the store an initial value even if it's undefined for { text?: string } then it works. @manucorporat

And I gave up on stackblitz, tried to create anything in that and nothing is working. Create a new repo, pushed my stuff there and loaded it with stackblitz.

Zeiver avatar May 12 '23 19:05 Zeiver