Skip to content

setting getters on already defined keys in a store loses reactivty #1450

@bigmistqke

Description

@bigmistqke

Describe the bug

Setting a getter-function on a key that has already been initialized in a store with setStore will not create a reactive getter, unlike when you set the getter-function while initializing a key.

This can be solved (as @thetarnav suggested) by doing a shallow merge.

setStore({
  alreadyDefinedKey: {
    get get() {
      return store.value;
    },
  },
});

but this feels like it should be supported without having to shallow merge objects (i personally try to avoid shallow merging as much as possible, because of the fact you can accidentally create multiple paths leading to the same reactive value and that can have unexpected results)

Your Example Website or App

https://playground.solidjs.com/anonymous/972b3aec-d7df-4ec0-9a68-c8b8b60785e3

Steps to Reproduce the Bug or Issue

const [store, setStore] = createStore({ value: 0, alreadyDefinedKey: {}});

setStore('alreadyDefinedKey', {
 get get() {
   return store.value
 }
})

setStore('newKey', {
 get get() {
   return store.value
 }
})

setStore('value', 1)

Expected behavior

I expect in the above example store.alreadyDefinedKey.get to be 1, just as store.newKey.get, but instead it is the old value 0.

Screenshots or Videos

No response

Platform

  • OS: iOS
  • Browser: firefox
  • Version: 12.1.1

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions