Actor
An actor is just a glorified action creator – "glorified" meaning it has one tiny property: type
. This is the type
property that should be set on all actions created by the actor.
Definition
interface Actor<T extends string> extends ActionCreator<T> {
type: T
toString(): T
}
type - Some string that identifies the actions created by this actor. Avoid using names starting with '@@zedux/'
as these are reserved for internal Zedux action types.
toString() - An actor's toString()
method should be overwritten with a function that returns the actor's type
.
Examples
That tiny type
property is a surprising asset. This one little detail is what eliminates string constants in Zedux. Instead of:
const INCREMENT = 'INCREMENT'
const increment = () => ({ type: INCREMENT })
const reducer = (state = 0, action) {
const amount = action.type === INCREMENT
return state + amount
}
we get:
const increment = () => ({ type: increment.type })
increment.type = 'increment'
// not necessary here, but for the sake of completeness:
increment.toString = () => increment.type
const reducer = (state = 0, action) {
const amount = action.type === increment.type
return state + amount
}
This doesn't seem like much in this example, but it allows us to create awesome apis like state()
and transition()
or act()
and react()
:
import { act, react } from 'zedux'
const increment = act('increment')
const reducer = react(0)
.to(increment)
.withReducers(state => state + 1)
Notes
While there's nothing wrong with putting actors together yourself, Zedux ships with a high-level api for creating them. See the ZeduxActor api for more info.