Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Enterprise React Development with UmiJS

You're reading from   Enterprise React Development with UmiJS Learn efficient techniques and best practices to design and develop modern frontend web applications

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781803238968
Length 198 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Douglas Alves Douglas Alves
Author Profile Icon Douglas Alves
Douglas Alves
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Part 1: Configuring UmiJS and Creating User Interfaces
2. Chapter 1: Environment Setup and Introduction to UmiJS FREE CHAPTER 3. Chapter 2: Creating User Interfaces with Ant Design 4. Chapter 3: Using Models, Services, and Mocking Data 5. Part 2: Protecting, Testing, and Deploying Web Applications
6. Chapter 4: Error Handling, Authentication, and Route Protection 7. Chapter 5: Code Style and Formatting Tools 8. Chapter 6: Testing Front-End Applications 9. Chapter 7: Single-Page Application Deployment 10. Other Books You May Enjoy

Using models for sharing states and logic

In this section, we'll create models for sharing states and logic between components.

A model is a special custom React hook to centralize the states and logic for a specific context.

We must create the models' files inside the src/models folder, and we can access these models using the useModel custom hook, as follows:

const { currentUser } = useModel('user');

Here, the user namespace matches the model filename, so the model file must be named user.ts.

Let's create the customer model and the opportunity model to demonstrate the use of models. These two models will contain the logic and result for creating, reading, and updating operations and share these operations between different interfaces.

Follow these steps to create the models:

  1. Create a new folder called models inside the src folder.
  2. Next, create a new file named customer.ts under the models folder and add the following:
    import...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime