Nerdiest Political Manifesto
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »


6.5.0 • Public • Published


State management for A-Frame using single global state modified through actions. Features declarative bindings to easily bind state to application. By separating state from components and being able to bind state to component properties, components can be decoupled from the application, not needing to know about its state.

No dependencies and tailored for A-Frame. Bindings will only update their entities if relevant pieces of the global state are modified.

v2 requires A-Frame v0.8.0.


  • [BeatSaver Viewer](


Defining State

The application state is a singleton defining an initial state and handler functions that modify the state.

  initialState: {
    score: 0
  handlers: {
    decreaseScore: function (state, action) {
      state.score -= action.points;
    increaseScore: function (state, action) {
      state.score += action.points;

Best practices is to keep the state purely state (numbers, strings, booleans) and functions that act on the state. Don't store or work upon entities in state (store IDs instead). State should be serializable and bindable.

Modifying State

To update the state, we can dispatch an action using an event:

AFRAME.scenes[0].emit('increaseScore', {points: 50});
// Or manually dispatched:
// AFRAME.scenes[0].systems.state.dispatch('increaseScore', {points: 50});

The binding components will automatically and selectively update the entities in response to state changes.

A stateupdate event will be fired, but we probably don't need to use it. The event might later be useful if we develop a debugging front-end for the state.

Binding State

Then we can declarative bind pieces of the state into the A-Frame application with the bind component. The components will be automatically updated when the state changes.

  <a-entity bind__text="value: app.score"></a-entity>
  <!-- Or <a-entity bind="text.value: score"> -->

There's a bind-toggle component which will attach and detach a component entirely based on a boolean value.

<a-entity bind-toggle__raycastable="isRaycastable"></a-entity>


Simple expressions are supported in binding.

NOT (!) and BOOL (!!):

<a-entity bind="visible: !enabled"></a-entity>
<a-entity bind__visible="!!enabled"></a-entity>

OR (||) and AND (&&):

<a-entity bind__menu="active: menuOpened && controllerEnabled"></a-entity>
<a-entity bind__gun="enabled: gunGrabbed && !!bullets || isRobot"></a-entity>

Comparisons (==, ===, !=, !==):

<a-entity bind__fly="enabled: animal === 'bird'"></a-entity>
<a-entity bind__legs="enabled: animal !== 'snake'"></a-entity>

Rendering Lists

The state component comes with a bind-for component that can render an array in state from a template. Say we have an array in state (currently must be at the root of the state):

  initialState: {
    shoppingList: [
      {name: 'milk', amount: 2},
      {name: 'eggs', amount: 12}

Note when updating the array in state, use array methods. Don't rewrite the array because the state component has wrapped the array to detect changes.

We use bind-for. We provide for (the iterator variable name), in (pointer to the array in state), and name (name of key representing unique identifier between every element). Then we have a <template> which will be used to render each individual item.

Then we can bind properties to the individual array element either using the bind component, using the for value as the pointer (i.e., Or we can use braces ({{ }}), which the bind-for component will statically interpolate the variable:

<a-entity bind-for="for: shoppingItem; in: shoppingList; key: name">
    <a-entity bind__text="value:"
              data-amount="{{ shoppingItem.amount }}"></a-entity>

This will result in:

<a-entity bind-for="for: shoppingItem; in: shoppingList; key: name">
    <!-- ... -->
  <a-entity bind__text="value:"
            text="value: milk"
  <a-entity bind__text="value:"
            text="value: eggs"

bind-for will automatically render new entities, remove old entities respective to changes in the array in the state. Updates are handled through individual binds.

Detecting Changes in Arrays

Arrays are references so the state system tries to detect when a change is made before re-rendering. Changes to the array itself are detecting by wrapping array methods and setting the __dirty flag.

Although detecting changes to objects within an array is a bit harder. For now, I developed a dirty solution. If you touch an object within an array, set state.myArray.__dirty = true;. Perhaps in the future, we will have dedicated methods for modifying arrays and objects in a way the state component can detect.

Computed State

To attach additional computed state after the action is processed, specify a computeState function to update the state:

  // ...
  computeState: function (newState, payload) {
    newState.isRedOrBlue = newState.isRed || newState.isBlue;


The component exports utilities to help organize the state definition by helping to split up handlers and compute state functions into separate modules.

composeHandlers (handlers1, handlers2, ...)

Combines together objects containing handlers. If two objects have a handler function for the same action name, those two handler functions will be composed together.

var composeHandlers = require('aframe-state-component').composeHandlers;
  computeHandlers: composeHandlers(
      quxAction: state => { state.counter++; }
      qazAction: state => { state.counter--; }

composeFunctions (fn1, fn2, ...)

Combines functions together. Useful for combining multiple computeState functions together.

var composeFunctions = require('aframe-state-component').composeFunctions;
  computeState: composeFunctions(

Optimizing State

If you are storing objects in state that don't need to be checked for changes nor binded to entities, then you can optimize the state component on updates as it checks for changes. By setting nonBindedStateKeys array, on state updates, these keys will be skipped when doing state diffing and copying.

  nonBindedStateKeys: ['starMap'],
  state: {
    starMap: {
      alphaCentari: {
        // ...
      // ...



Install and use by directly including the browser files:

  <title>My A-Frame Scene</title>
  <script src=""></script> 
  <script src="^3.0.0/dist/aframe-state-component.min.js"></script> 
      initialState: {
        enemyPosition: {x: 0, y: 1, z: 2}
      handlers: {
        enemyMoved: function (state, action) {
          state.enemyPosition = action.newPosition;
    <a-entity bind__position="enemyPosition"></a-entity>


Install via npm:

npm install aframe-state-component

Then register and use.



npm i aframe-state-component

Downloadsweekly downloads









last publish


  • avatar
Report a vulnerability