question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't reset state when passing a reference object as the initial value

See original GitHub issue

Reproduction

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
posvacommented, May 9, 2021

This is expected. Reset only calls the function to create a new object. If possible, build the object inside of the state method

0reactions
quantuminformationcommented, Apr 1, 2022

ah great thx

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found