This is unreleased documentation for Front-Commerce Developers next version.
For up-to-date documentation, see the latest version (3.x).
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.
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.
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.