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

How to easily reset state? #83

Open
flashman2 opened this issue Nov 15, 2022 · 1 comment
Open

How to easily reset state? #83

flashman2 opened this issue Nov 15, 2022 · 1 comment

Comments

@flashman2
Copy link

I didn't found a way to easily reset global state to initial values.

Currently, to reset a state I need to reset each property of global state object. For a big state objects it is not an easy way. Is it possible to reset global state object with single command/function? Below is my code to reset a state.
Note: Strange, but array can be reset with new array only. Reset not works with globalStateInit.teamsCurrentTimeouts. Not sure why.

Example:

const globalStateInit = {
  teamsCurrentScores: {
    home: 0,
    visitor: 0,
  },
  teamsCurrentColors: {
    home: {
      background: 'red',
      text: 'blue',
    },
    visitor: {
      background: 'yellow',
      text: 'green',
    },
  },
  teamsCurrentTimeouts: {
    home: [false, false],
    visitor: [false, false],
  },
  
};

const {setGlobalState, useGlobalState} = createGlobalState(globalStateInit);

export const resetState = () => {
  setGlobalState(
    'teamsCurrentScores',
    () => globalStateInit.teamsCurrentScores,
  );
  setGlobalState(
    'teamsCurrentColors',
    () => globalStateInit.teamsCurrentColors,
  );
  setGlobalState('teamsCurrentTimeouts', prev => ({
    ...prev,
    home: [false, false],
    visitor: [true, false],
  }));
};
@dai-shi
Copy link
Owner

dai-shi commented Nov 15, 2022

It's somewhat intentional to make bulk changes uneasy.
Yes, I think resetting each property is the right solution in this library.

array can be reset with new array only.

It's probably cased by some other code mutates the array, which should be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants