# Components in React, Vue and Angular

> How to pass props, listen to events and handle a late-loading script when you use a Taggor component inside a React, Next.js, Vue or Angular app.

A Taggor component is a custom element (`<price-list>`). To your framework it is an HTML tag, like `<video>`.
Three things change from framework to framework.

1. **Props.** An HTML attribute carries only text. A text, number or boolean prop works as an attribute
   (`heading="Plans"`). A list or an object (`plans`) has to go as a **property** of the element
   (`element.plans = [...]`); as an attribute it becomes the text `"[object Object]"`, the component
   refuses it and uses the default value.
2. **Events.** The component tells the page with events named `<tag>:<name>` (`price-list:select`), and the
   data is in `event.detail`. `addEventListener` works in every framework; each framework's own syntax does
   not always accept the `:`.
3. **When the component arrives.** The Taggor script is asynchronous: usually the tag is defined **after**
   your app has rendered. The element is already on the page and is activated in place when the component
   arrives; a property you set before is not lost.

## Summary

| Framework | Complex prop (`plans`)                                                                    | Event `price-list:select`                                                   | Component arrives after the app                   | SSR                                                                                                         |
| --------- | ----------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| React 18  | `ref` + effect (`el.plans = plans`)                                                       | `addEventListener` on the `ref`                                             | Works                                             | n/a                                                                                                         |
| React 19  | Direct (`plans={plans}`) **only if the tag is already defined**; otherwise `ref` + effect | `addEventListener` on the `ref`, or the prop `onprice-list:select`          | Direct: fails until the next render. `ref`: works | Hydration sets neither the direct prop nor `onprice-list:select`: use `ref` + effect and `addEventListener` |
| Vue 3     | `:plans.prop="plans"`                                                                     | `@price-list:select` or `addEventListener`                                  | Works (with `.prop`)                              | n/a                                                                                                         |
| Angular   | `[plans]="plans"` with `CUSTOM_ELEMENTS_SCHEMA`                                           | `addEventListener` (the template's `(price-list:select)` does **not** work) | Works                                             | n/a                                                                                                         |

In all of them the app can re-render freely. While the tag stays in the same place of the tree, the framework
reuses the same element and the component only receives the new props, keeping its own state. What
**remounts** the component, and clears its state, is taking the tag out of the tree and putting it back:
changing the `key` (React), toggling with `v-if` (Vue) or `@if` (Angular).

## React 18

To skip the recipes of this section and the next, generate typed React components for your site's components
with `taggor codegen react`: [React and Next.js](https://taggor.dev/docs/platforms/react-next.md#typed-react-components).

React 18.3.1, with the component defined before or after the first render.

React 18 passes every prop of a custom element as an attribute and has no syntax for an event with `:`. The
recipe: a `ref`, the complex prop in an effect and the event with `addEventListener`.

```tsx
import { useEffect, useRef, useState } from "react";

export function Plans({ plans }: { plans: Plan[] }) {
  const ref = useRef<HTMLElement & { plans?: Plan[] }>(null);
  const [selected, setSelected] = useState<string>();

  // Complex prop: a property, on every change.
  useEffect(() => {
    if (ref.current) ref.current.plans = plans;
  }, [plans]);

  // Event with ":": addEventListener, with cleanup.
  useEffect(() => {
    const element = ref.current;
    if (!element) return;
    const onSelect = (event: Event) => setSelected((event as CustomEvent).detail.id);
    element.addEventListener("price-list:select", onSelect);
    return () => element.removeEventListener("price-list:select", onSelect);
  }, []);

  return <price-list ref={ref} heading="Plans" />;
}
```

A text prop (`heading`) goes as an attribute, the normal way. It works with the component defined before or
after the first render.

## React 19 and Next.js

React 19.3.1; Next.js 16.4 (App Router, static export) in Chromium, Firefox and
WebKit ([Browser and platform compatibility](https://taggor.dev/docs/browser-support.md)).

React 19 sets the prop of a custom element as a **property** when the element already has it
(`"plans" in element`), that is, when the tag was defined before React created the element.

```tsx
<price-list heading="Plans" plans={plans} />
```

This works if the component is already defined. **But the Taggor script usually arrives later.**
Then React 19 writes the attribute `plans="[object Object],[object Object]"`, the component shows the empty
list (the default) and only receives the plans on the next render of your React component. So for a
complex prop, use the React 18 recipe (`ref` + effect), which works either way.

**Event.** Besides `addEventListener`, React 19 understands a prop of `on` + the event name on a custom
element: `onprice-list:select` listens to `price-list:select`. In JSX, the `:` in the name asks for a
spread: `<price-list {...{ "onprice-list:select": onSelect }} />`.

**SSR (Next.js).** This is the page **without** Taggor's server rendering, which is described in
[Server-side rendering](https://taggor.dev/docs/ssr.md). The server renders the app with
`react-dom/server` (`renderToString`) and the browser hydrates with `hydrateRoot` (React 19, development
mode, which shows hydration warnings). The components run on a real Next.js App
Router export, for rendering, props, events and slots.

- The server sends the tag with only the text props (`<price-list heading="Round 1">`), no shadow root, no
  `plans` and no listener. The component mounts only in the browser. So there is no hole until then, use
  [fallback content](https://taggor.dev/docs/fallback.md).
- Hydration gives **no warning** of a mismatch: React does not compare the shadow root the component creates
  (with the component defined before or after the hydration; a changed server text does warn).
- Hydration does **not set** the direct prop `plans={plans}` and does not attach `onprice-list:select`:
  both only take effect on the next render. With `ref` + effect and `addEventListener`, everything
  works from the hydration on. In Next.js (App Router), that code goes in a React component with
  `"use client"`.

## Vue 3

Vue 3.5.44, with render functions that set the property with the `.prop` modifier and the
listener a template's `@price-list:select` compiles to. The template below is the form those render functions
stand for.

```vue
<price-list
  heading="Plans"
  :plans.prop="plans"
  @price-list:select="(event) => (selected = event.detail.id)"
/>
```

- **`.prop`** forces the property even before the component arrives. Without it, Vue uses the property only
  when `"plans" in element`; before the definition, it writes the attribute.
- **`@price-list:select`** works: the template compiles to the listener `onPriceList:select`, which Vue
  attaches to the event `price-list:select`. `addEventListener` on a `ref` also
  works.
- **Declare the tag as a native element** so that Vue does not look for a Vue component named `price-list`,
  for example `compilerOptions.isCustomElement: (tag) => tag === "price-list"` in the Vue plugin's config.

### Typed tags, and Nuxt

`taggor codegen vue` writes declarations that type the tags of your site in Vue templates: `vue-tsc` and the
editor check the props and the `@<tag>:<event>` listeners, and a template ref is the typed element. The npm
package `@taggor/vue` is a plugin that adds the site's snippet once.

```sh
npm install @taggor/vue
npx taggor login
npx taggor codegen vue --site st_abcdefghij --dir src/taggor
```

`--site` takes the site's id or one of its two site keys; a site key also says the environment whose versions
are read. The folder holds `<tag>.d.ts` and `<tag>.vue.d.ts` for each component and an `index.ts` with
`isTaggorElement`. Commit it, add it to `.prettierignore`, and run the command again when a component of the
site changes. In CI, the same command with `--check` exits with a non-zero code when the folder is out of date.

**Vite.** Give `isTaggorElement` to the Vue plugin, so Vue renders the site's tags as elements, and install
the Taggor plugin with the site key:

```ts
// vite.config.ts
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";

import { isTaggorElement } from "./src/taggor/index.ts";

export default defineConfig({
  plugins: [vue({ template: { compilerOptions: { isCustomElement: isTaggorElement } } })],
});
```

```ts
// src/main.ts
import { createTaggor } from "@taggor/vue";
import { createApp } from "vue";

import App from "./App.vue";

createApp(App)
  .use(createTaggor({ siteKey: "st_abcdefghij" }))
  .mount("#app");
```


**Nuxt.** Generate into `app/taggor`, pass `isTaggorElement` in `nuxt.config.ts` and install the plugin in a
client plugin (a file that ends in `.client.ts`):

```ts
// nuxt.config.ts
import { isTaggorElement } from "./app/taggor/index.ts";

export default defineNuxtConfig({
  vue: { compilerOptions: { isCustomElement: isTaggorElement } },
});
```

```ts
// app/plugins/taggor.client.ts
import { createTaggor } from "@taggor/vue";

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(createTaggor({ siteKey: "st_abcdefghij" }));
});
```

Then write the tag in a template:

```vue
<finance-simulator
  :rate="1.2"
  :terms.prop="[12, 24, 36]"
  style="min-height: 420px"
  @finance-simulator:simulate="(event) => (installment = event.detail.installment)"
/>
```

- A prop of the wrong type (`rate="x"`) is an error in `vue-tsc` and in the editor.
- **Reserve the space.** Nuxt's server sends the tag with its attributes (`<finance-simulator rate="1.2">`)
  and the component appears when the Taggor script runs in the browser. Give the element the height it will
  have and the page does not move when it arrives. To have the rendered component in the server's HTML, see
  [Server-side rendering](https://taggor.dev/docs/ssr.md).

## Angular

Angular 22.2.3, with the JIT compiler and without the Angular CLI.

```ts
@Component({
  selector: "app-plans",
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `<price-list #list heading="Plans" [plans]="plans()"></price-list>`,
})
export class PlansComponent {
  readonly plans = signal<Plan[]>([]);
  readonly selected = signal<string | undefined>(undefined);
  private readonly list = viewChild.required<ElementRef<HTMLElement>>("list");

  constructor() {
    const onSelect = (event: Event) => this.selected.set((event as CustomEvent).detail.id);
    afterNextRender(() =>
      this.list().nativeElement.addEventListener("price-list:select", onSelect),
    );
    inject(DestroyRef).onDestroy(() =>
      this.list().nativeElement.removeEventListener("price-list:select", onSelect),
    );
  }
}
```

- **`CUSTOM_ELEMENTS_SCHEMA`** lets the template use a tag Angular does not know.
- **`[plans]`** is a property binding: Angular always sets the property, with the component defined or not.
- **`(price-list:select)` does not work**: Angular reads the `:` as a global target (`window:`, `document:`,
  `body:`) and stops with the error "Unexpected global target 'price-list' defined for 'select' event".
  Use `addEventListener` after the first render, as above, finding the element
  with `querySelector` instead of `viewChild`.
