You can create a reusable component in Astro by creating a new .astro file in your src/components/ directory. This component can accept properties (props) which you can use to customize the component's behavior each time it's used.
Here's how you can create a reusable component for your footer links:
-
Create a new file at the location src/components/FooterLinks.astro.
-
Copy the following code into your new file, FooterLinks.astro.
---
const { title, links } = Astro.props;
---
<div class="flex flex-col gap-2">
<h6 class="text-primary">{title}</h6>
<div class="flex flex-col gap-y-2 text-sm text-primary-40">
{links.map(link => (
<a href={link.href} class="hover:text-primary transition-colors w-max">
{link.text}
</a>
))}
</div>
</div>
In this component, title is the title of the footer section, and links is an array of objects, where each object has href and text properties for the link URL and link text, respectively.
You can then use this component in your pages like so:
---
import FooterLinks from '../components/FooterLinks.astro';
const links = [
{ href: '/about', text: 'About Us' },
{ href: '/contact', text: 'Contact Us' },
// add more links as needed
];
---
<FooterLinks title="About" :links={links} />
This will create a footer section with the title "About" and two links "About Us" and "Contact Us". You can reuse the FooterLinks component multiple times with different titles and links.
As for performance, hardcoding values can be faster, but it reduces the reusability of your components. By using props, you can reuse the same component with different data, which can make your code more maintainable and easier to read.