๐ฆ 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.name
can be replaced withname
if 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
Hello
component is imported and used like a regular JSX component. - We pass the
name
prop ("Sara"
), which gets displayed inside theHello
component.
โป๏ธ 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 default
to 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.