Creating a Component
This documentation is currently under development.
In this guide, you'll learn how to create a component from scratch and use it in your FastStore project. For the purpose of this guide, we'll create a Price
component to display the price of a product in the Product Details
component.
The FastStore Price component displays the price of a given product, discount and total values | The custom Price component created can display any information that you set. In this case it renders the name Price |
Step by step
Step 1: Create the Price
file
- Open your FastStore project in a code editor and navigate to
src
folder. - Create a new folder called
components
inside thesrc
folder.
This structure is suggested, but it's not required. You can place the component in any subdirectory of the src
folder.
- Inside the
components
folder, create a new file calledPrice.tsx
. - Export the following function in the
Price.tsx
file:
export default function Price(props: any) {
return <div>Price</div>;
}
This function creates the new Price
component. When this component is used, it displays the word Price
on the screen. You can change what the component displays by editing the information within the <div>
tags.
To use the new Price
component, you need to override the default Price
component by following the next step.
Step 2: Override the default Price
component
Now, let's override the component to render it in the Product Details page.
- Inside the
components
folder, create a new folder calledoverrides
. - In the
overrides
folder, create a new file called, for example,CustomPrice.tsx
. - Export the following function in the
CustomPrice.tsx
file to override thePrice
component:
import CustomPrice from '../Price'
export default {
name: "ProductDetails",
components: {
Price: CustomPrice,
},
};
The above function defines the name of the component (ProductDetails
) and which component to override (Price
). In this case, we are importing the CustomPrice
component from the ../Price
file.
- Save the changes and the new
Price
component will be rendered in the browser with the content defined in thePrice.tsx
file (in this case, the textPrice
).