# All files / lib create-state.js

90.47% Statements 38/42
50% Branches 2/4
50% Functions 1/2
90.47% Lines 38/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    421x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1ximport { $$hooks } from './util/symbols';
    import { ActiveRenderState } from './util/types';

    /**
     * Create state allows a class or function component to create state from the
     * render function. Any other attempt to use this will fail. All createState
     * invocations must be in the exact same order, every render.
     *
     * Borrowed from the concept introduced by React, useState. This will be used by
     * the React Compatibility later renamed as `useState`. The reason this is named
     * `createState` is due to no guarentees of parity with React hooks.
     *
     * @param {any} defaultValue
     *
     * @returns {any[]}
     */
    export function createState(defaultValue = {}) {
      if (ActiveRenderState.length === 0) {
        throw new Error('Cannot create state unless in render function');
      }

      const [ activeComponent ] = ActiveRenderState;
      const activeHook = activeComponent[$$hooks].shift();
      const currentValue = activeHook ? activeHook[0] : defaultValue;
      const retVal = activeHook || [currentValue];

      /**
       * @param {any} newValue
       */
      const setState = newValue => {
        retVal[0] = newValue;
        activeComponent.forceUpdate();
      };

      if (retVal.length === 1) {
        retVal.push(setState);
      }

      // Return currentValue and setState.
      activeComponent[$$hooks].push(retVal);
      return retVal;
    }