Skip to content

[✨] self-referencing QRLs (was: Allow binding this to the store in useStore) #293

@wmertens

Description

@wmertens

Is your feature request related to a problem?

It would be nice to have have functions in stores be bound to the store object.

Describe the solution you'd like

I think a bind option to useStore would be nice. It could even be true by default because the majority use case for functions in the store is using the store object itself.

This can be used orthogonally with deep and reactive.

import { component$, useStore, $, useTask$ } from '@builder.io/qwik';

export default component$(() => {
  const store = useStore({
    count: 1,
    inc: $(function () {console.log('I have a this:', !!this);this.count++})
  }, {bind: true})

  return <p>Count: {store.count} <button onClick$={store.inc}>one up</button></p>;
});

Describe alternatives you've considered

letting the deserializer do it, but then the function is only bound when it's deserialized.

Additional context

This allows making zustand-like APIs.

UPDATE

See this comment to see how to do it manually, but it would be nice if the optimizer did it.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Released as Stable (STAGE 5)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions