#Lesson 4 - Components
This section covers building the components the e-commerce project needs and attaching them to the correct models.
A component is a reusable set of fields defined once in the schema and embedded in models as a basic or modular component field. The components built here solve two specific problems in this project. The Product Variant modular component makes it possible for a single Product model to handle clothing, shoes, accessories, and decor, each with its own required fields, without creating four separate models. The Stripes modular component on the Landing page model gives editors the ability to compose pages from a variable combination of CTA and Product Grid sections.
By the end of lesson 4.2, the following models are updated:
Product—Product VariantandRelated Productscomponents attachedLanding page—Stripesmodular component attachedNavigation—Nav Linkcomponent attached, completing the model left open in lesson 1.2
#Components and component fields
A component and a component field are two different things. You create the component first, then embed it in a model using a component field.
Hygraph supports two types of component fields:
| Type | What it does |
|---|---|
| Basic component field | Embeds a single component type. Use this when the field always contains the same structure. |
| Modular component field | Allows editors to choose from a set of component types. Use this when editors need to select which component fills the field. |
The product type components (Clothing, Shoes, Accessories, Decor) use a modular component field inside Product Variant because the content editor needs to present a choice. The Related Products component uses a basic component field because no choice is required. It always contains the same structure.
#Build order matters
Some components depend on others. Build them in this order:
- Related Products
- Clothing, Shoes, Accessories, Decor (in any order)
- Product Variant (depends on 2)
- Button
- Call to Action (depends on 4)
- Product Grid
- Link component modification (pre-built in the cloned project)
For the complete components documentation, see Components.
#Lessons in this section
| Lesson | What you will do |
|---|---|
| 4.1 Build components | Create components and configure their fields, references, and nested component fields |
| 4.2 Add components to your models | Attach the components to the Product, Landing page, and Navigation models to complete the schema |
#What's next
Lesson 4.1 - Build componentsOr, go to the Tutorial overview for the full lesson list.