Local State Management 2
Definitions
Before we dive into state management, this chapter gives you general definitions and definitions for
state managements to build up a common vocabulary for state management for this book. It should
help you to follow the book effortlessly when reading it without leaving space for confusion.
Pure Functions
Pure functions is a concept from the functional programming paradigm. It says that a pure function
always returns the same output if given the same input. There is no layer in between that could alter
the output on the way when the input doesn’t change. The layer in between, that could possibly alter
the output, is called side-effect. Thus, pure functions have no side-effects. Two major benefits of
these pure functions are predictability and testability.
Immutability
Immutability is a concept of functional programming, too. It says that a data structure is immutable
when it cannot be changed. When there is the need to modify the immutable data structure, for
instance an object, you would always return a new object. Rather than altering the object at hand,
you would create a new object based on the old object and the modification. The old and new object
would have their own instances.
Immutable data structures have the benefit of predictability. For instance, when sharing an object
through the whole application, it could lead to bugs when altering the object directly, because every
stakeholder has a reference to this potentially altered object. It would be unpredictable what happens
when an object changes and a handful of stakeholders are dependent on this object. In a growing
application, it is difficult to oversee the places where the object is currently used by its reference.
The antagonist of immutability is called mutability. It says that an object can be modified.
State
State is a broad word in modern applications. When speaking about application state, it could be
anything that needs to live and be modified in the browser. It could be an entity state that was
retrieved from a backend application or a view state in the application, for instance, when toggling
a popup to show additional information.
I will refer to the former one as entity state and to the latter one as view state. Entity state is data
retrieved from the backend. It could be a list of authors or the user object describing the user that
is currently logged in to the application. View state, on the other hand, doesn’t need to be stored in
the backend. It is used when you open up a modal or switch a box from preview to edit mode.
When speaking about managing the state, meaning initializing, modifying and deleting state, it will
be coined under the umbrella term of state management. Yet, state management is a much broader