Skip to content Skip to sidebar Skip to footer

How To Achieve A Maintainable, Reactive UI With Vanilla JS

today I've got a problem which would be easily solved by using a reactive and state managed framework like Vue. Sadly, its not posssible to use it. Following (simplified) situation

Solution 1:

I followed my thoughts by implementing internal state with a render-function which holds all UI related changes.

const RenderMode = {
  INITIAL: "Initial",
  CREATE: "Create",
  OPEN: "Open",
  SHOW_NOTE: "Show note input",
  TOGGLE_PRICE: "Toggle price input",
};

render() {
    switch (this.renderMode) {
      case RenderMode.INITIAL:
        this._hideIndicatorIcon();
        this._hideIndicatorRow();
        this._hideInputRow();
        this._hidePriceInput();

        break;
      case RenderMode.CREATE:
        this._showInputRow();
        break;
      case RenderMode.OPEN:
        this._showIndicatorIcon();
        this._hideInputRow();
        this._hideIndicatorRow();
        break;
      case RenderMode.SHOW_NOTE:
        this._showIndicatorRow();
        break;
      case RenderMode.TOGGLE_PRICE:
        this._togglePriceInputs();
        break;
      default:
        console.error("No render mode defined!");
    }

State after page reload is determined from custom attributes of server side rendered html:

  initializeRenderMode() {
    ...
    // if note already exists on page load switch render mode to open
    this.renderMode = existingNote ? RenderMode.OPEN : RenderMode.INITIAL;
    this._render();
  }

Its by far not the best solution, but it helps me to keep things simple.


Post a Comment for "How To Achieve A Maintainable, Reactive UI With Vanilla JS"