Overview

Purpose

The actions object is what you use to create Riduce-standard actions through the create API.

In useRiducer

useRiducer returns a { state, dispatch, actions } object where the dispatch function updates state on the basis of Riduce-standard actions produced by actions, using a riduce-produced reducer under-the-hood.

In riduce

riduce returns a [reducer, actions] pair because the reducer is wired up to precisely listen to the Riduce-standard actions produced by actions.

Usage

Arbitrary property paths

actions can take an arbitrary path of properties after it, which typically correspond to a 'leaf' (Leaf state) at the corresponding path from your state. (It is typed to have the same shape as your Tree state.)

import riduce from 'riduce'

const initialState = {
  counter: 0,
  arbitrary: {
    nested: {
      path: ['hi!']
    }
  }
}

const [reducer, actions] = riduce(initialState)

// state.counter
console.log(typeof actions.counter) // 'object'

// state.arbitrary.nested
console.log(typeof actions.arbitrary.nested) // 'object'

// state.arbitrary.nested.path
console.log(typeof actions.arbitrary.nested.path) // 'object'

// but also works for paths not in your initial state
console.log(typeof actions.not.in.my.initial.state) // 'object'

Accessing create

For a given arbitrary property path, you have two options: navigating to a deeper property / leaf, or accessing the create property and API.

// Access create at the state root
console.log(typeof actions.create) // 'function'

// Go deeper
console.log(typeof actions.arbitrary) // 'object'

// Access create at state.arbitrary
console.log(typeof actions.arbitrary.create) // 'function'

// Go deeper
console.log(typeof actions.arbitrary.nested.path)

// Access create at state.arbitary.nested.path
console.log(typeof actions.arbitrary.nested.path.create) // 'function'

Once you've accessed create, you can't go arbitrarily deeper beyond that.

console.log(typeof actions.create.arbitrary) // 'undefined'

This is because the create key accesses the Riduce create API at the corresponding leaf of state.