get [] sometimes #1885
Answered
by
dbritto-dev
vaynevayne
asked this question in
Bug report
get [] sometimes
#1885
-
Beta Was this translation helpful? Give feedback.
Answered by
dbritto-dev
Jun 26, 2023
Replies: 3 comments 4 replies
-
|
Can you create a reproduction with https://codesandbox.io/s/react-new ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
const init = {
sortOrder:['1','b']
}
const appStoreSortOrder = useAppStore(state => state.sortOrder )
console.log('render', appStoreSortOrder) // ['1','b']
const fn =()=>{
const sortList = appStoreSortOrder
while (sortList.length) {
const shift = sortList.shift()
}
}
When fn is executed, the appStore becomes [] Is this normal behavior? Is there any way to prevent unexpected changes? For example, zusTand's readonly STate |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
@dai-shi What's the situation now? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, only primitives are readonly (you can't mutate them, just reassign them) in JS. What you are doing is mutating an object directly (which is wrong in a react context). BTW, what you get from
useStoreis not the store itself if just the result of a given selector, even if the selector if the "store" itself. Nothing avoids mutate that result (object) directly but is wrong in a react context due to that mutation only exists on the scope component so if you read the store on another component the va…