1 of 22

12 - React state management.

Speck Academy 2021

2 of 22

React state management

Mobx

1

2

Overview

3 of 22

1

React state management

4 of 22

React State Management overview

State

Component state

Context API

State Management Solutions

5 of 22

State

In an application, state is an interface between your data from any kind of backend or local change and the representation of this data with UI elements in the frontend.

State is able to keep data of different components in sync because each state update will re-render all relevant components.

State can be a medium to communicate between different components aswell.

In React there are many different paradigms and libraries to keep your state managed.

6 of 22

Component state

State inside the component

  • The most straightforward way is to define a state inside the component.
  • The state of a component is like the props which are passed to a component, a plain JS object containing information that influences the way a component is rendered.
  • In comparison to the props, state can be changed by component itself.
  • State API of React is really simple at all and doesn’t add too much complexity to your application.

Besides all other state management solutions, the component state is preferred to not be replaced at all because you should always keep your state as close to where it is needed to avoid unnecessary complexity.

7 of 22

Component state

It is totally possible to write your application just with component state, but as your component dependencies and the size and complexity of your app grows, dealing with state will become more complex.

In more complex apps, distance between your state locations and your components could increase.

This leads to props drilling, meaning passing props through components which don’t need the props but their children do.

We need to avoid this because it increases the complexity in development and also in refactoring.

8 of 22

Context API

The Context API provides an internal solution for passing state where it is needed and avoids the possibility of props drilling.

  • If we want to use Context API we must define Provider that provides a certain component state to all Consumers that are located somewhere in the React component tree below the provider.
  • Beneath the state, a provider can also provide functions to manipulate the state within the provided values.
  • The Consumer accepts a render function and is able to access the value prop from the Provider

Context API is perfect solution to pass data that is not so complex.

9 of 22

State Management Solutions

As you can see, component state and Context API has their own purpose, and both of the solutions are adequate to some specific use cases.

Context API has a lot of possible use cases, also for more complex situations, but there are also libraries that are made only for more complex cases.

When you need to manage state of applications with higher complexity try to decide between one of the following libraries:

  • Redux
  • MobX

Read more about Redux vs Mobx.

10 of 22

2

Mobx

11 of 22

MobX overview

MobX basics

Core concepts & implementation

12 of 22

MobX basics

MobX is a library that makes state management simple and scalable by applying functional reactive programming.

MobX is using a reactive virtual dependency state graph that is only updated when strictly needed and is never stale.

React and MobX together are a powerful combination.

  • React renders the application state by providing mechanisms to translate it into a tree of renderable components.
  • MobX provides the mechanism to store and update the application state that React then uses.

13 of 22

MobX basics

MobX makes impossible to produce an inconsistent state, because everything that can be derived from the application state, will be derived.

14 of 22

MobX basics

State - graphs of objects, arrays, primitives, references that forms the model of your application. These values could be considered as the “data cells” of your app.

Computed values (derivations) - any value that can be computed automatically from the state of your application (simple values like number of unfinished todos, to complex stuff like a visual HTML representation of todos).

Reactions - react to state changes and produce side effect like updating the UI.

Actions - only thing that modify state (invoked by events). MobX will make sure that all changes to the application state caused by actions are automatically processed by all computed values and reactions.

15 of 22

Core concepts & implementation

With MobX we can add observable capabilities to existing data structures like objects, arrays and class instances.

If we are using app created with create-react-app then its easiest way to use decorate function to add decorators to the our store.

Using observable is like turning a property of an object into a spreadsheet cell.

The observable is telling our MobX store what will be state property.

16 of 22

Core concepts & implementation

Another core concept is computed value that defines value that will be derived automatically when relevant data is modified.

For computed values computed decorator must be used and also we need to define it with get keyword in front of computed method.

If we compare observables with the spreadsheet cells, then computed values can be compared with formulas in spreadsheet programs like MS Excel.

17 of 22

Core concepts & implementation

Third main concept is action that is needed to modify our store state.

In order to decorate action, use action decorator.

On the right side you can see example of simple store for storing Events inside events array.

  • By using setEvent action you can put new event inside store state
  • By computed eventsLength() method, number of all events can be returned to any component that uses that computed value.

18 of 22

Core concepts & implementation

In the component that we want to use our previously defined store we need to assure that:

  • Store is passed through props from parent component, by use of Context API or it is directly imported inside component.
  • Our component uses observer from mobx-react or mobx-react-lite library.

19 of 22

3

Hands-on

20 of 22

Hands-on

21 of 22

4

Teamwork

22 of 22

Teamwork

Link to Teamwork

You can find mistakes from 2020 generation in this document.