Integrate

React and Next.js

A React or Next.js app renders one slot after the answer has finished streaming. The component on this page is illustrative, and the real package is issued after the traffic check.

Last updated · 7 min read

On this page

A React or Next.js chat app renders one slot after its own answer component. The slot waits until the reply has finished streaming. If the account is marked under 18, the slot does not render. If Visor returns nothing, the thread shows nothing in that spot.

The component on this page reuses the illustrative shape from the homepage and from Quickstart. It is a picture of the idea. It is not an install command. Placement is the rule. This page is how that rule meets a component tree.

The illustrative component

Answers is your component. AdSlot stands where the ad goes, after those words. publisherId, messages, locale, and country show the idea of the call. They are not a field list for a live API. messages stands for the current conversation. country on a live request is the United States. The label stays Ad · Sponsored either way. locale in the sample does not change that wording.

Illustrative: component shapetsx
import { AdSlot } from "@visor/react";

export function Thread({
  messages,
}) {
  return (
    <>
      <Answers messages={messages} />
      <AdSlot
        publisherId="YOUR_PUBLISHER_ID"
        messages={messages}
        locale="en-US"
        country="US"
      />
    </>
  );
}

Do not drop AdSlot inside a map over paragraphs or tokens. That paints a slot per piece of the answer. Map the bubbles, then render one slot after the finished answer.

Package name and install

SDK access, including the real package name and install source, is issued with your integration docs after the traffic check. Do not run an install from this page. There is no public install command here. Web, iOS, Android, React, and Node packages are issued to approved apps. Their install steps are on the locked SDK page.

Visor does not call your model provider, and it does not need that provider’s key. Do not put a model key in the component, in props, or in the messages you pass for matching. Any model can sit behind Answers. The provider is your choice.

Render only when the answer is done

The shape above shows where the slot sits. It does not show the stream. Gate it with a flag you own. The samples call that flag done. When done is false, render the answer only. When done is true, and the account is not marked under 18, render one slot under the answer.

Returning null from the slot component leaves no node. That is what you want during streaming, on a refusal, on an error, on an empty answer, and on no-fill if the issued component does not already collapse itself. Do not return an empty box with a minimum height. Placement covers the empty state. Follow it.

Next.js App Router

In the App Router, components are server components unless the file opts into the client. The slot follows a stream that finishes in the browser. Put the slot in a client component. The answer can stay in a server component if that is how the page is built.

The client boundary

Add "use client" at the top of the file that renders the slot. Pass messages, done, and under18 in as props. Do not import the illustrative slot into a file that also runs server-only data loading, unless that file is itself a client component. Keep the boundary small: the answer’s text can be computed above it, and the slot decides whether to mount.

Illustrative: client slot and server turntsx
"use client";

import { AdSlot } from "@visor/react";

// Illustrative. done and under18 are your props.
// The env var name is illustrative. The package name is illustrative.
export function SlotAfterAnswer({ messages, done, under18 }) {
  if (!done || under18) return null;

  const publisherId =
    process.env.NEXT_PUBLIC_VISOR_PUBLISHER_ID ?? "YOUR_PUBLISHER_ID";

  return (
    <AdSlot
      publisherId={publisherId}
      messages={messages}
      locale="en-US"
      country="US"
    />
  );
}

SlotAfterAnswer is the client file. Turn is the server file. Answers is yours. The import path ./slot-after-answer is yours. Nothing in those names is a published Visor export except the illustrative AdSlot import, and that import is still a sample.

What stays on the server

A server component can render the answer and can decide, from data you already have, whether the account is marked under 18. Pass that boolean down. Do not pass the person’s name, email, or phone number into the client slot so the ad code can “know who they are.” Matching does not use those. The privacy policy is the list.

If the materials you are issued include a value that must stay on the server, do not put it in a client component and do not prefix it with NEXT_PUBLIC_. Server-side integration is the page for a request that leaves from your backend. The browser sample on this page is for a publisher ID the client is allowed to hold.

The publisher ID in an environment variable

NEXT_PUBLIC_VISOR_PUBLISHER_ID is an illustrative name. The real publisher ID is the one issued after the traffic check. A NEXT_PUBLIC_ variable is inlined into the browser bundle by Next.js. Use that pattern only if your integration docs say the publisher ID can live in the client. Do not put a secret in it.

The ?? "YOUR_PUBLISHER_ID" fallback in the sample is there so the shape is readable. Delete the fallback before you ship. A placeholder sent as live traffic is not a publisher ID. If the variable is missing, render nothing and fix the configuration.

A streaming status flag

Chat libraries often expose a status for the current reply. A hook in the style of useChat may report that the reply is still streaming, or that it is ready. Treat the ready state as the moment the answer is finished. Read the values your library actually returns. The comparison below is a sketch of that idea.

Visor does not publish an adapter for that hook, or for any other vendor. There is no Visor provider to drop into a chat SDK. You own the flag. When the status says the reply is finished, set done. When it says the reply failed, keep done false and skip the slot.

Illustrative: your status flagtsx
// Illustrative. status belongs to your chat hook.
// A hook in the style of useChat often reports a ready state
// when the reply has finished, and a streaming state before that.
// Read the spelling your library actually returns.
// Visor does not publish an adapter for any vendor.
const done = status === "ready";

Do not copy the string "ready" into production unless that is the string your version of the library uses. Spelling changes between libraries and between versions. The duty does not change: wait until the stream has finished, then mount one slot.

Accounts marked under 18

If the account is marked under 18, do not render the slot and do not request an ad. The sample encodes that as under18, which is your prop, not a published component prop. A publisher can mark an account as under 18. We then do not request an ad. The prop is how your UI keeps the same promise even before a request exists.

Do not send a birth date, a child’s name, or other personal information to make the mark. Apps directed at children under 13 are not a fit for the network at all. The privacy policy states both lines. Turn ads off for every account marked under 18 before the first ad, as onboarding describes.

Avoid a reserved gap

Do not give the slot a minimum height while you wait. An empty box is a reserved gap. When an ad arrives, the thread grows by the height of that ad. That movement is acceptable. A blank reservation is not. Do not scroll the answer out of view to make room. Do not pin the ad over the text the person is reading.

If you animate the arrival, keep it in your own UI and keep it short. Do not move the answer to announce the ad. Respect prefers-reduced-motion in that animation. We do not publish a motion specification for publisher apps. The ad is the next block, or it is absent.

Strict mode and double renders

React Strict Mode invokes some lifecycle behavior twice in development. If you request an ad from an effect, the second run must not paint a second slot. Render one component under the answer, and let that component be idempotent. Do not append a slot to the DOM from an effect that can run twice.

Prefer rendering AdSlot in the tree, gated by done, over calling a loader imperatively. The tree is easier to keep at one slot. If the issued integration tells you to call a function instead, call it once per finished answer and make a second call a no-op while that answer is current.

How to test the component

  • Stream a reply and confirm no slot appears until done is true.
  • Mark under18 and confirm the slot does not mount.
  • Force a refusal and an empty answer. Confirm the slot stays unmounted.
  • Force an empty result and confirm the thread closes up, with no gap.
  • Confirm the label, when an ad is present, reads Ad · Sponsored at the top-left.
  • Confirm the region is an aside named Advertisement, unless the issued snippet already provides that landmark.
  • Walk the rest of the list on Testing and go-live.

Ask for test ads during onboarding when you need a real creative. Do not point a live publisher ID at your own clicks. How test traffic is marked is set in your integration docs. Testing covers that check.

Was something unclear? Contact support