Skip to main content
Version: next

Create a Business Component

In this guide, we learn how to build a Business Component. The core concept is the same as creating a UI component.

In Front-Commerce we have separated our components in two categories: the UI components available in the app/theme/components folder, and the Business components available in the app/theme/modules and app/theme/pages folders.

note

If you would like to understand why we went for this organization, feel free to refer to React components structure first.

What is a Business component

A Business component will be located in the app/theme/modules folder. Those components are not meant to be reused a lot in your application, they are built with a very specific use in mind. When creating a custom theme, they should emerge in your Pages components.

To illustrate this, imagine that you are building the homepage for your store. You add a big hero image on top, some product listings for news and sales, a reinsurance banner, etc.

Quickly, you will want to extract some components from your page to avoid a big bloated file. Some of these components will be extracted as reusable UI components but some are very specific to your page and there is no reason to put them in the components folder.

note

They are often a mix between UI components and custom layout. They may be split into multiple components if they are big enough.

Generally, they are related to your business and often need backend data like CMS content or store information. We refer to them as Business components or even modules.

note

Unlike UI components, Business components are often smart and contain logic. We try to extract this logic in hooks, but more on that later.

Creating a store locator

To explain the concept and the emergence of modules, we will add a store locator to our home page and see how to extract it properly as a module.

In the following steps, we are going to build our store locator and we will go through:

  1. Displaying a map on the homepage
  2. Fetching the position of the store from the backend
  3. Link both to have an actual module

Using it in our App

We now have extracted all the store locator logic. We can now use our brand new and shiny module component within the homepage.

app/theme/pages/Home/Home.tsx
import StoreLocator from "theme/modules/StoreLocator";

const Home = () => (
<div className="page page--home">
{/* ... */}
<StoreLocator />
</div>
);

As you can see, we did not use a relative import. This is because in Front-Commerce we have a few aliases that will let you import files without worrying about your current position in the folder structure.

In our case, the Home component being in app/theme/pages/Home/Home.js, we do not have to import the StoreLocator by using relative paths ../../modules/StoreLocator but we can remain with app/theme/modules/StoreLocator which is more explicit. This is possible for any file located in the folder theme/theme of an extension.

And it works! You now have a clean Home page component that uses a Business component which could be used anywhere in your application (About us, Contact, etc.).

Going further

The store locator we just created is very simple and it has a very limited Business impact. The store locator module does not need to know the implementation of the map itself (like the fact of using react-leaflet). So a map component could be extracted in a UI component. But for the sake of this guide, we kept it simple.

As a final note, please keep in mind that splitting code is a difficult task. It needs practice and refinement. But it is also a pretty personal point of view. Thus one team could split code differently. In this guide we have made a choice but feel free to make yours.

In any case, we advice you to not overcomplicate things and find a method that matches your needs.