DocsLearn
Your first component
A Sluurp app is made of React components: functions that return what to show, written in JSX. Every example on these pages runs as you edit it.
In this lesson
- how to write a component and use it inside another;
- how JSX differs from HTML;
- how to style with Tailwind classes;
- how to pass a component values with props.
A component is a function
export default function App() {
return <h1 className="text-2xl font-semibold">Hello, world</h1>;
}Change the text and the result follows. Three things to notice:
- A component’s name starts with a capital letter:
App,Card,PostList. That’s how JSX tells your component from an HTML tag like<h1>. - It returns JSX: markup inside JavaScript. Bun compiles it as the app is served, so nothing has to be set up.
export defaultis the component this example shows. In an app, you export your components from their own files and import them where they’re used.
Components inside components
A component is used like a tag. Give it a closing slash when it has nothing inside: <Avatar />.
function Avatar() {
return <div className="grid size-10 place-items-center rounded-full bg-sky-100 font-medium text-sky-700">AL</div>;
}
export default function App() {
return (
<div className="flex items-center gap-3">
<Avatar />
<div>
<p className="font-medium">Ada Lovelace</p>
<p className="text-sm text-muted-foreground">Mathematician</p>
</div>
</div>
);
}JSX in a few rules
- One parent. A component returns one element. Several sit in a
<div>, or in<>…</>when no element should wrap them. - Every tag is closed:
<img />,<br />,<input />. - Curly braces hold JavaScript:
<p>{1 + 1}</p>shows2, and<img src={user.photo} />takes a value. className, notclass, andhtmlForinstead offor: JSX uses the names JavaScript gives them.- Events are attributes that start with
on:onClick={() => …}. More in Events and forms.
Styling
Tailwind classes work in every file with no setup: the server makes the CSS for the classes your code uses. Components brings ready ones, buttons, inputs, dialogs and more, styled the same way.
import { Button } from "@/components/ui/button";
export default function App() {
return (
<div className="flex flex-wrap items-center gap-2">
<Button>Save</Button>
<Button variant="outline">Cancel</Button>
<Button variant="ghost">Later</Button>
</div>
);
}Combining classes with cn
cn builds a class string from pieces. It does two things:
- It joins. Pass strings, conditions, arrays or objects. Falsy values are dropped, and an object’s keys are kept where their value is true.
- It resolves conflicts. When two classes set the same thing, the last one wins:
cn("px-2", "px-4")gives"px-4". Without this, which one applies would depend on the order of the stylesheet, not on yours.
import { cn } from "cn";
cn("rounded-md px-3", "font-medium"); // "rounded-md px-3 font-medium"
cn("px-3", isActive && "bg-primary text-white"); // the second part only while isActive is true
cn("px-3", { "opacity-50": disabled, ring: focused }); // each key kept when its value is true
cn("p-4 text-sm", "p-2"); // "text-sm p-2": the later padding wins
cn("px-2 hover:px-4", "px-6"); // "hover:px-4 px-6": a variant is separateThis is what lets a component take a className from whoever uses it and let it win over its own defaults:
import { useState } from "react";
import { cn } from "cn";
function Tag({ className, children }) {
return <span className={cn("rounded-full bg-muted px-3 py-1 text-sm", className)}>{children}</span>;
}
export default function App() {
const [on, setOn] = useState(false);
return (
<div className="flex flex-wrap gap-2">
<Tag>Default</Tag>
<Tag className="bg-sky-100 text-sky-800">Coloured</Tag>
<Tag className="px-12">Wider</Tag>
<button className={cn("px-3 py-1 text-sm", on ? "bg-emerald-600 text-white" : "bg-muted")} onClick={() => setOn(!on)}>
{on ? "On" : "Off"}
</button>
</div>
);
}bg-sky-100 replaces the tag’s bg-muted, and px-12 replaces its px-3, while the rest stays. The components work the same way, so <Button className="w-full"> widens a button without losing its look. See Styling and Tailwind.
Props
A component receives values from the one that uses it, as attributes. They arrive together as its first argument, usually destructured right away:
function Greeting({ name, excited = false }) {
return <p className="text-lg">Hello, {name}{excited ? "!" : "."}</p>;
}
export default function App() {
return (
<div>
<Greeting name="Ada" />
<Greeting name="Grace" excited />
</div>
);
}excited = falseis used when noexcitedis given.excitedwritten alone meansexcited={true}.
More in Props and children.
Where components go
In an app, a component lives in its own .tsx file and is imported where it’s used:
export function Greeting({ name }) {
return <p>Hello, {name}.</p>;
}import { Greeting } from "@/components/greeting";
export default () => <Greeting name="Ada" />;A page in routes/ is drawn on the server; a component that changes in the browser says "use client" at its top. See Server components.
Next: make something change on screen, with state.