State

The State class wraps an object and makes it immutable by allowing access to it only through State's API.


constructor(object)

Creates a new State object, with the initial state set to the value of object.

Arguments

object (Object)

The initial state.

Returns

(State) A new State object.


get(key)

Retrieves the value stored at key.

Arguments

key (String)

The key to retrieve.

Returns

(any) The value, or undefined if the key doesn't exist.


set(key, value)

Sets the value at key.

Arguments

key (String)

The key to set.

value (any)

The value to set.

Returns

(State) The modified State object. The original State object is not changed.


update(key, updater)

Calls updater on the value at key, and sets the value at key to the result. Equivalent to state.set(key, updater(state.get(key))).

Arguments

key (String)

The key to update.

updater (Function)

The function to call on the value at key.

Returns

(State) The modified State object. The original State object is not changed.


getIn(keyPath)

Retrieves the value at a key path.

state = new State({
  a: {
    b: 'Hello!'
  }
})

state.getIn(['a', 'b']) // -> 'Hello!'

Arguments

keyPath (Array)

The key path to retrieve.

Returns

(any) The value, or undefined if the key doesn't exist.


setIn(keyPath, value)

Sets the value at a key path.

state = new State({
  a: {
    b: 'Hello!'
  }
})

newState = state.setIn(['a', 'b'], 'Bonjour!')

newState.getIn(['a', 'b']) // -> 'Bonjour!'

Arguments

keyPath (Array)

The key path to retrieve.

value (any)

The value to set.

Returns

(State) The modified State object. The original State object is not changed.

results matching ""

    No results matching ""