Part 1: Local State Management

Filip Ilievski

May 10, 2021

The post is part of a presentation I gave at my employer, MOIA, as part of our "Continuous Learning" sessions and as the title suggests, it's about managing local state in the front-end, with a comparison of Redux and Apollo Client, and how we can carry out sate management using Apollo Client.

The "Why?"

When we talk about Local State Management we refer to a pattern of attaching primitive and non-primitive properties that are only accessible on the client. The goal of this is to enable our components (the view layer) to subscribe to a part of the store, get notified when that part changes, and at the end, to re-render.

There are many open source state management libraries with decent comminity around them that are well mentained. Five years ago, the go-to solution was Redux. Today, there are others, but we will focus on Apollo Client in this post.

What is common for these librareis is that the data we store in them is actually global. So, here comes the question - What is global state and what are the benefetis?

Global State

As the name suggest, this is just an object that is globally accessible and can be update from anywhere within the client app. There are some benefits that are worth mentioning:

It is worth mentioning that the terminology “Global state” is not that great because the general interpretation of “Global state” in most user-oriented apps is “a storage for all data your app is detailing with”.

Types Of State

As you would guess by the title of the section - there are several types of state, but for the sake of simplicity, let us divide them into client and server states.

Main differences between client and server states:

Client state is:

Server state is:

With this in mind we have a good picture about the types of states.

Now comes the definition of declarative and imperative paradigm and I am sure most of you know it, but as it is the key difference between the Apollo Client and Redux, I would like to point it out.

In simple terms, declarative - it should look like this and imperative - this is what you should do.

Problem with Redux

Then we have the Apollo Client which is not the way Redux is!

Apollo Client state management

In Part 2: State Management in Apollo Client, we will look deeper into how we can use Apollo Client for state management.