Skip to content

How to easily reset state? #83

@flashman2

Description

@flashman2

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],
  }));
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions