๐ฆ SeraJS Reusable Components Guide โ
This guide helps you understand how to create and use reusable components in SeraJS.
๐คฉ Component: Hello โ
โ Purpose โ
A simple component to display a greeting message using a name prop.
๐ File Structure โ
/project-root
โโ App.jsx
โโ Hello.jsx๐ Hello.jsx โ
jsx
import { h } from "serajs";
// Option A: Destructured props
export default function Hello({ name }) {
return <h1>Hello {name}</h1>;
}
// Option B: Access via object
export default function Hello(person) {
return <h1>Hello {person.name}</h1>;
}
person.namecan be replaced withnameif you use the destructured props version (Option A). Both approaches are valid โ choose based on your preference or project standards.
๐ App.jsx โ
jsx
import { h } from "serajs";
import Hello from "./hello";
export default function App() {
return <Hello name="Sara" />;
}The
Hellocomponent is imported and used like a regular JSX component. We pass thenameprop ("Sara"), which gets displayed inside theHellocomponent.
โป๏ธ How to Make Components Reusable โ
- Use props: Pass data into components using props.
- Avoid hardcoding: Keep content dynamic using the props passed.
- Export components: Use
export defaultto make components available for import. - Import with path: Import components relative to the file path (
./hello).
๐งช Example with Multiple Props โ
jsx
export default function Welcome({ name, age }) {
return (
<p>
Welcome {name}, Age: {age}
</p>
);
}Usage:
jsx
<Welcome name="Alex" age={25} />โ Best Practices โ
- ๐งผ Keep components small and focused.
- ๐ฆ Group related components in folders.
- ๐งช Test components individually before integration.
- ๐ Document props with JSDoc or comments.