MobX Quick Start Guide
eBook - ePub

MobX Quick Start Guide

Supercharge the client state in your React apps with MobX

  1. 236 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

MobX Quick Start Guide

Supercharge the client state in your React apps with MobX

About this book

Apply functional Reactive programming for simple and scalable state management with MobX

Key Features

  • The easiest way to learn MobX to enhance your client-side state-management
  • Understand how the concepts and components fit together
  • Work through different state management scenarios with MobX

Book Description

MobX is a simple and highly scalable state management library in JavaScript. Its abstractions can help you manage state in small to extremely large applications. However, if you are just starting out, it is essential to have a guide that can help you take the first steps. This book aims to be that guide that will equip you with the skills needed to use MobX and effectively handle the state management aspects of your application.

You will first learn about observables, actions, and reactions: the core concepts of MobX. To see how MobX really shines and simplifies state management, you'll work through some real-world use cases. Building on these core concepts and use cases, you will learn about advanced MobX, its APIs, and libraries that extend MobX.

By the end of this book, you will not only have a solid conceptual understanding of MobX, but also practical experience. You will gain the confidence to tackle many of the common state management problems in your own projects.

What you will learn

  • Explore the fundamental concepts of MobX, such as observables, actions, and reactions
  • Use observables to track state and react to its changes with validations and visual feedback (via React Components)
  • Create a MobX observable from different data types
  • Define form data as an observable state and tackle sync and async form validations
  • Use the special APIs to directly manipulate observables, tracking its changes, and discovering the reasons behind a change
  • Tackle any state management issue you may have in your app by combining mobx-utils and mobx-state-tree
  • Explore the internals of the MobX reactive system by diving into its inner workings

Who this book is for

This book is for web developers who want to implement easy and scalable state management for their apps. Knowledge of HTML, CSS, and JavaScript is assumed

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access MobX Quick Start Guide by Pavan Podila, Michel Weststrate in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over one million books available in our catalogue for you to explore.

Handling Real-World Use Cases

Applying the principles of MobX can seem daunting when you first start using it. To help you with this process, we are gong to tackle two non-trivial examples of using the MobX triad of observables-actions-reactions. We will cover the modeling of the observable state, and then identify the actions and the reactions that track the observables. Once you go through these examples, you should be able to make the mental shift in tackling state management with MobX.
The examples we will cover in this chapter include the following:
  • Form validation
  • Page routing

Technical requirements

You will be required to have JavaScript programming language. Finally, to use the Git repository of this book, the user needs to install Git.
The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Mobx-Quick-Start-Guide/tree/master/src/Chapter06
Check out the following video to see the code in action:
http://bit.ly/2LDliA9

Form validation

Filling up forms and validating fields is the classic use-case of the web. So, it's fitting we start here and see how MobX can help us simplify it. For our example, we will consider a User Enrollment form that takes in some standard inputs like first name, last name, email, and password.
The various states of enrollment are captured in the following figure:

The interactions

Looking at the preceding screenshot, we can see some standard interactions going on, such as:
  • Entering inputs for various fields
  • Validation on those fields
  • Clicking the Enroll button to perform a network operation
There are few other interactions here that do not meet the eye immediately:
  • Network-based validation for the email to ensure that we are not registering with an existing email address
  • Showing a progress indicator for the enroll operation
Many of these interactions will be modeled with actions and reactions in MobX. The state will, of course, be modeled with observables. Let's see how the Observables-Actions-Reactions triad comes to life in this example.

Modeling the observable state

The visual design for the example already hints at the core state that we need. This includes the firstName, lastName, email, and password fields. We can model these as observable properties of the UserEnrollmentData class.
Additionally, we also need to track the async validation that will happen with email. We do that with the boolean validating property. Any errors that are found during the validation are tracked with errors. Finally, the enrollmentStatus tracks the network operation around enrollment. It is a string-enum that can have one of four values: none, pending, completed, or failed:
class UserEnrollmentData {
@observable email = '';
@observable password = '';
@observable firstName = '';
@observable lastName = '';
@observable validating = false;
@observable.ref errors = null;
@observable enrollmentStatus = 'none'; // none | pending | completed | failed
}
You will notice that errors is marked with @observable.ref, as it only needs to track reference changes. This is because the validation output is an opaque object, which does not have anything observable besides a change in reference. Only when errors has a value do we know that there are validation errors.

Onto the actions

The actions here are quite straightforward. We need one to set the field value based on user changes. The other is do enrollment when the Enroll button is clicked. These two can be seen in the following code.
As a general practice,...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Foreword
  6. Contributors
  7. Preface
  8. Introduction to State Management
  9. Observables, Actions, and Reactions
  10. A React App with MobX
  11. Crafting the Observable Tree
  12. Derivations, Actions, and Reactions
  13. Handling Real-World Use Cases
  14. Special API for Special Cases
  15. Exploring mobx-utils and mobx-state-tree
  16. Mobx Internals
  17. Other Books You May Enjoy