quarrant / mobx-persist-store

Persist and rehydrate observable properties in mobx store.
267 stars 14 forks source link

Question: adding a custom variable name instead of harcoded properties name #102

Open alaa-paramount opened 1 year ago

alaa-paramount commented 1 year ago

hi guys,

I'm facing the following situation:

testVar = "abc";

makePersistable(
    this,
    {
        properties: ['testVar'],
        // rest of stuff
    }
);

I'm trying to pass a variable name to 'properties', but it's not working, also, I've tried to do it like the following code but it didn't work: properties: [Object.keys(this.testVar)[0] as keyof this],

my goal is to not hardcode strings inside properties[] any suggestions?

codeBelt commented 1 year ago

Can you add an https://codesandbox.io/ example of what you are trying to do and I can look at it.

alaa-ananey commented 1 year ago

Thanks @codeBelt,

here's the codesnadbox: https://codesandbox.io/p/sandbox/todostore-ts-sepdzu?file=%2Fstore%2FTodoStore.ts&selection=%5B%7B%22endColumn%22%3A52%2C%22endLineNumber%22%3A22%2C%22startColumn%22%3A52%2C%22startLineNumber%22%3A22%7D%5D

file: TodoStore.ts scroll to line 21: properties: ["activeNote"]

my goal is to make it work with variable, something like this: properties: [this.activeNote]

ThaJay commented 1 year ago

@alaa-ananey

    makePersistable(this, {
      name: 'ChangeAddressStore',
      properties: Object.keys(this) as (keyof this)[]
    })