Module: common/stateReflector

Methods

(static) stateReflector(getState, setState(o)) → {function}

Track the state of an object and reflect it to and from the URL.
Parameters:
Name Type Description
getState function Function that returns an object representing the state we want reflected to the URL.
setState(o) function Function to call when the URL has changed and the state object needs to be updated. The object 'o' doesn't need to be copied as it is a fresh object.
Source:
Returns:
A function to call when state has changed and needs to be reflected to the URL.
Type
function
Example
// If an element has a private variable _state:
this._state = {"foo": "bar", "count": 7}

// then in the connectedCallback() call:
this._stateHasChanged = stateReflector(
  () => this._state,
  (state) => {
    this._state = state;
    this._render();
  }
);

// And then any time the app changes the value of _state:
this._stateHasChanged();