Kea 2.1: Less squggly bits ๐ and previous state in listeners ๐ฆ๐ฆ
Marius Andra
Kea Core TeamKea 2.1 does two things:
- Continues the "let's make things simpler" trend started in Kea 2.0
by removing another bunch of squiggly bits that you will not need to type again:
" ((((((()))))))===>>>{}"
- Adds support for accessing the state before an action was fired inside listeners.
It's also backwards compatible: Logic written for Kea version 1.0 will still run in 2.1.
#
The saga until now:This is Kea 1.0:
In Kea 2.0 we can skip [actions.]
and { actions }
:
You can still write [actions.]
explicitly... and you do it mostly when
using actions from another logic:
... but you save 41 keystrokes in the default case:
#
Changed in Kea 2.1:Why stop there?
There's another low hanging fruit we can eliminate: () => ({})
.
Gone!
16 units of squiggly bits gone! Here they are, in chronological and ascending order:
They are there if you need them, of course. For example when using
props
in reducers
:
What about the selectors? How can we simplify this?
Here's the simplest backwards-compatible change that went into Kea 2.1:
Goodbye another 14 spaces and squgglies:
If you're really feeling the minimalist vibe, you could also simplify
the object in listeners
and events
, but:
You might get tired of writing thisLogic
everywhere.
In general, the suggestion is to always write the simplest thing first:
... and only when needed, extend it into a function to pull in objects and evaluate lazily:
#
Previous State in ListenersThere's a certain way listeners work:
The action
will first update the reducers
and only then run the listener
.
What if you really need the state before the action ran?
You could set up a two step system (setFloorStart
& setFloorUpdate
)
... or you could use previousState
, the new 4th argument to listeners:
Take the store's previousState
(new 4th argument) and run it through any
selector
you can get your hands on. Every value
has a selector
, so you
have plenty to choose from.
How does this work?
This is just another benefit of using Redux under the hood. More specifically, using the idea redux popularised: store everything in one large tree and propagate changes in its branches through cascading immutable updates.
Every unique version of the entire state tree ends up in a plain JS object
.
This state object is only read from and it will never change... and it's discarded
as soon as the next state
comes in.
We can still keep a reference to this previous state and use selectors on it to get whichever selected or computed value we need.
Easy as pie!
Mmm... pie. ๐ฐ
4th argument?
Yeah, it's getting busy up there, but ๐คท. I'm not going to make a breaking change for this.