Can't reset state when passing a reference object as the initial value
See original GitHub issueReproduction
I’m not 100% sure if this is the expected behaviour but when I define the store like this:
export const counterInitialState = {
counter: 0,
};
const useCounter = defineStore({
id: 'counterStore',
state: () => counterInitialState,
actions: {
increment() {
this.counter++;
},
decrement() {
this.counter--;
},
},
});
If I do counterStore.$reset()
it doesn’t reset the store.
I can fix it by not using the initial state variable or by making a copy of the initial state using object spreading like:
state: () => {...counterInitialState}
or if the state is too nested:
state: () => JSON.parse(JSON.stringify(counterinitialState))
Expected behavior
Should reset the store to the initial state
Actual behavior
Nothing happens
Additional information
Demo repo: https://github.com/slauzinho/pinia-reset-store-example
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Reset to Initial State with React Hooks - Stack Overflow
There is no built-in way to set the state to its initial value, sadly. Your code looks good, but if you want to...
Read more >Java is Pass by Value, Not Pass by Reference | DigitalOcean
Technically, Java is always pass by value, because even though a variable might hold a reference to an object, that object reference is...
Read more >TypeError: Reduce of empty array with no initial value
The JavaScript exception "reduce of empty array with no initial value" occurs when a reduce function is used.
Read more >Referencing Values with Refs - React Docs
Inside your component, call the useRef Hook and pass the initial value that you want to reference as the only argument. For example,...
Read more >Resetting Variables to Their Initial Value - SAP Help Portal
The CLEAR statement resets a reference variable to its initial value, that is, so that it does not point to an object.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is expected. Reset only calls the function to create a new object. If possible, build the object inside of the state method
ah great thx