Skip to content

๐Ÿ“ฆ 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 with name 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 the Hello component.

โ™ป๏ธ How to Make Components Reusable โ€‹

  1. Use props: Pass data into components using props.
  2. Avoid hardcoding: Keep content dynamic using the props passed.
  3. Export components: Use export default to make components available for import.
  4. 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.

๐Ÿ“š Resources โ€‹