Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial value of all store #30

Closed
wants to merge 1 commit into from

Conversation

aralroca
Copy link
Collaborator

@aralroca aralroca commented Nov 4, 2021

With this change this useStore(initialValue) (or with getStore / withStore) is valid. However, is doing an Object.assign with the parameter + the current store, this way only add the missing store variables and is not removing the existint ones.

const { getStore } = createStore({ cart: { price: 0 }})
getStore({ username: 'Aral' })
// Now the store is { username: 'Aral', cart: { price: 0 } }
// Now the initial store is { username: 'Aral', cart: { price: 0 } }

The difference with setStore is that setStore is replacing all the store
without touching the initialStore:

const { getStore } = createStore({ cart: { price: 0 }})
const [,setStore] = getStore()
setStore({ username: 'Aral' })
// Now the store is { username: 'Aral' }
// The initial store is the same that before: { cart: { price: 0 } }

Proposal for future releases: RenamegetStore to another name because do more things than get... For example myStore...

With this change this `useStore(initialValue)` (or with  getStore / withStore) is valid. However, is doing an Object.assign with the parameter + the current store, this way only add the missing store variables and is not removing the existint ones.

````js
const { getStore } = createStore({ cart: { price: 0 }})
getStore({ username: 'Aral' })
// Now the store is { username: 'Aral', cart: { price: 0 } }
// Now the initial store is { username: 'Aral', cart: { price: 0 } }
```

The difference with setStore is that setStore is replacing all the store
without touching the initialStore:

```js
const { getStore } = createStore({ cart: { price: 0 }})
const [,setStore] = getStore()
setStore({ username: 'Aral' })
// Now the store is { username: 'Aral' }
// The initial store is the same that before: { cart: { price: 0 } }
```
@aralroca aralroca added the bug Something isn't working label Nov 4, 2021
@aralroca aralroca added this to the 0.6.1 milestone Nov 4, 2021
@aralroca aralroca self-assigned this Nov 4, 2021
@aralroca
Copy link
Collaborator Author

aralroca commented Nov 4, 2021

I need to reduce 2 bytes 😅

image

@danielart
Copy link
Collaborator

go for that 2 bytes! 💪

@aralroca
Copy link
Collaborator Author

aralroca commented Nov 13, 2021

I close this PR because with this other PR there is no more initial value, so the same can be done directly with setStore(store => ({ ...store, newProperty: 'blabla' })). And it's already normal that useStore({ someValue: 2 }) does not work, because as it already exists the store does not change it. So it only works for properties (useStore.someValue(2)). What I will do is explain it better in the documentation.

@aralroca aralroca closed this Nov 13, 2021
@aralroca aralroca deleted the aral-fix-initial-value-all-store branch November 13, 2021 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants