Sitemap
Andrew Solomon

Andrew Solomon

doing manly things... Like wrestling bears and taking out the garbage

Complex State

--

Typescript is a great resource when planning and building an application that will hold complex state. Typescript gives us a precursor into the shape of our state and therefore makes complex state a little bit easier to manage. As discussed in Simple State post, creating state is a as simple as assigning a primitive data type, but what exactly is complex state.

What is Complex State?

A complex state refers to a state that consists of multiple values, often represented as an object. This can include arrays, nested objects, or any other non-primitive data type.

For instance, consider this example:

interface Player {  
name: string;
position: string;
team: string;
}

let player: Player = {
name: "Josh Allen",
position: "Quarterback",
team: "Buffalo Bills"
};

In this example, we’ve created an interface Player with three properties: name, position, and team. We then assign a Player object to the variable player. This is a clear example of complex state, as it involves storing multiple values in an object.

Get Andrew Solomon’s stories in your inbox

Join Medium for free to get updates from this writer.

Using the interface we can quickly see that our object consists of 3 primitive values. These primitive values may change overtime, we can change all the values at once, or we can change the values independently. Having that granular level of control over this data structure is what state management is all about.

Nested Complex State

Nesting state is the idea of introducing non-primitive datatypes to your complex state structure like in the following example:

type Player = {
id: number;
name: string;
position: string;
};

type Team = {
id: number;
name: string;
players: Player[];
};

In the above example we have aPlayer object which holds complex state, but we also have a Team object which holds complex state by way of encapsulating the Player object.

Complex state can be infinitely complex. We can have a complex data structure inside of complex data structure inside of another complex data structure. Believe it or not this is how we build most apps when using state management. Most React or view apps may have a top level data structure representing the entire state of the app commonly referred as Application State or AppState for short.

AppState is usually a nested complex state like the following example:

type Player = {
id: number;
name: string;
position: string;
};

type Team = {
id: number;
name: string;
players: Player[];
};

type AppState = {
teams: Team[];
};

const initialState: AppState = {
teams: [
{
id: 1,
name: "Red Team",
players: [
{ id: 1, name: "John Doe", position: "Forward" },
{ id: 2, name: "Jane Smith", position: "Midfielder" },
{ id: 3, name: "Alex Johnson", position: "Defender" },
],
},
{
id: 2,
name: "Blue Team",
players: [
{ id: 4, name: "Sarah Williams", position: "Goalkeeper" },
{ id: 5, name: "Mike Thompson", position: "Midfielder" },
],
},
],
};

As you can see, complex state structures can quickly become a monolith. We will dive into state management in later blog posts, but for this post we want to establish what a complex state is and what it looks like.

Conclusion

Creating a system for state management starts with understanding how your type system will shape. Using TypeScript to create your initial state structure, will make state management easier when your ready to create your state management system. In future blog posts we will use these foundations to build modular systems.

--

--

Andrew Solomon
Andrew Solomon

Written by Andrew Solomon

doing manly things... Like wrestling bears and taking out the garbage