Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
React Components

You're reading from   React Components Explore the power of React components for cutting-edge web development

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781785889288
Length 182 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
 Pitt Pitt
Author Profile Icon Pitt
Pitt
Arrow right icon
View More author details
Toc

Dependency injection and service location


Dependency injection and service location are interesting concepts that are not limited to React development. To really understand them, let's move away from components for a while. For a moment, imagine that we wanted to create a sitemap. To do this, we could perhaps use code resembling the following code:

let backend = {
    getAll() {
        // ...return pages
    }
};
class SitemapFormatter {
    format(items) {
        // ...generate xml from items
    }
}
function createSitemap() {
    const pages = backend.getAll();
    const formatter = new SitemapFormatter();
  
    return formatter.format(
        pages.filter(page => page.isPublic)
    );
}

let sitemap = createSitemap();

In this example, createSitemap has two dependencies. Firstly, we fetch pages from backend. This is a kind of global storage object. We used something similar to this when we looked at the Flux architecture.

The second dependency is to the SitemapFormatter implementation...

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 $15.99/month. Cancel anytime
Visually different images