Modular State Management: View Actions

Episode #74 • Sep 30, 2019 • Subscriber-Only

It’s time to fully modularize our app! Our views can still send any app action, so let’s explore transforming stores to focus in on just the local actions a view cares about.

View Actions
Introduction
00:06
Transforming a store’s action
00:40
Combining view functions
06:35
Focusing on favorite primes actions
08:45
Extracting our first modular view
11:27
Focusing on prime modal actions
13:43
Focusing on counter actions
15:24
Next time: what’s the point?
22:11

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

And just like that our views now take a smaller subset of state than the full AppState, which means the stores that power the views are starting to look a lot more like the reducers that power them.

It’s also worth noting how simple these changes were, and how quick we made them. We merely changed the state that the views operated on to focus in on just what they needed, and then we made sure that the stores we passed to those views were transformed in order to pluck out these values.

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in

Exercises

  1. It can be useful to produce “read-only” stores that cannot send any actions. Write a view that transforms a store that can perform actions into a store that cannot perform actions. What is the appropriate data type to describe the Action of such a store?

    In our second episode on algebraic data types, we explored such a transformation.

  2. In our first episode on algebraic data types, we introduced the Either type, which is the most generic, non-trivial enum one could make:

    enum Either<A, B> {
      case left(A)
      case right(B)
    }
    

    In this episode we create a wrapper enum called CounterViewAction to limit the counter view’s ability to send any app action. Instead of introducing an ad hoc enum, refactor things to utilize the Either type.

    How does this compare to utilizing structs and tuples for intermediate state?

References

Contravariance

Brandon Williams & Stephen Celis • Monday Apr 30, 2018

We first explored the concept of the pullback in our episode on “contravariance”, although back then we used a different name for the operation. The pullback is an instrumental form of composition that arises in certain situations, and can often be counter-intuitive at first sight.

Let’s explore a type of composition that defies our intuitions. It appears to go in the opposite direction than we are used to. We’ll show that this composition is completely natural, hiding right in plain sight, and in fact related to the Liskov Substitution Principle.

Why Functional Programming Matters

John Hughes • Saturday Apr 1, 1989

A classic paper exploring what makes functional programming special. It focuses on two positive aspects that set it apart from the rest: laziness and modularity.

Access Control

Apple

This chapter of the Swift Programming Language book explains access control in depth and how it affects module imports.

Elm: A delightful language for reliable webapps

Elm is both a pure functional language and framework for creating web applications in a declarative fashion. It was instrumental in pushing functional programming ideas into the mainstream, and demonstrating how an application could be represented by a simple pure function from state and actions to state.

Redux: A predictable state container for JavaScript apps.

The idea of modeling an application’s architecture on simple reducer functions was popularized by Redux, a state management library for React, which in turn took a lot of inspiration from Elm.

Composable Reducers

Brandon Williams • Tuesday Oct 10, 2017

A talk that Brandon gave at the 2017 Functional Swift conference in Berlin. The talk contains a brief account of many of the ideas covered in our series of episodes on “Composable State Management”.