qwik icon indicating copy to clipboard operation
qwik copied to clipboard

[🐞] Reactivity is inconsistent when returning data from custom hooks

Open cmbartschat opened this issue 2 years ago • 2 comments

Which component is affected?

Qwik Runtime

Describe the bug

Logic that works in the body of a component, when moved to a custom hook, gives strange results.

Reproduction

https://stackblitz.com/edit/qwik-starter-nbqrb9?file=src/routes/index.tsx

Steps to reproduce

  • Under "With hook", click on the "add" button.
  • See that items in the list start appearing, but the The following list has 0 items. text doesn't change.
  • The other two implementations do not have this issue.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @builder.io/qwik: ^0.102.0 => 0.102.0 
    @builder.io/qwik-city: ^0.102.0 => 0.102.0 
    undici: 5.21.0 => 5.21.0 
    vite: 4.2.1 => 4.2.1

Additional Information

https://discordapp.com/channels/842438759945601056/1097515413929214012/1097515413929214012

cmbartschat avatar Apr 19 '23 06:04 cmbartschat

This seems the result of a bad optimization that assumes use methods always return the same value, it's a very powerful optimization but seems to break your custom hook.

For this use case i would look into maybe using a useStore() it will always return the same value and you can achieve the same API!

manucorporat avatar Apr 19 '23 14:04 manucorporat