withState Class

Decorate your route with the ability to transition between (internal) states. That state will be purged upon entering and exiting the route so you do not need to manage it.

Here's an example that tracks an internal counter and outputs it to the console when it changes:

import { withState } from 'page-fu';

export default withState({
  getInitialState() {
    return { value: 0 };
  },

  enter() {
    this.renderValue(); // => "Value = 0"
    this.increment();
  },

  stateDidChange() {
    this.renderValue(); // => "Value = 1"
  },

  renderValue() {
    console.log('Value = %d', this.state.value)
  },

  increment() {
    this.setState({
      value: this.state.value + 1
    });
  }
})

Constructor

withState(instance: Route) -> Route

Parameters (1)

instance
Route

Return Value

Route

Static Methods

Static Properties

state: Object

The route's internal state which can be mutated using the state routines.

Instance Methods

    getInitialState() -> Object

    A producer for the initial state the container should start in. The output of this method will be used when clearing the container as well.

    Return Value

    Object

    The initial state definition.

    setState(partialState: Object)

    Transition to a new state, overwriting any existing keys.

    Parameters (1)

    partialState
    Object

    replaceState(newState: Object)

    Transition to an entirely new state, replacing the old one.

    Parameters (1)

    newState
    Object

    clearState()

    Reset the state.

    stateDidChange(prevState: Object)

    A hook that is invoked when the state changes through calls to withState#setState, withState#replaceState or withState#clearState.

    Parameters (1)

    prevState
    Object