in-app-ui
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKnock in-app UI skill
Knock内嵌UI技能指南
This skill helps you build in-app UI with Knock. It covers the two in-app products — feeds and guides — at a high level, then goes deep on guides: provider setup, rendering with hooks, and debugging.
Reference: https://docs.knock.app/in-app-ui/overview
本技能指南帮助你使用Knock构建内嵌UI。它概览了两款内嵌产品——feeds和guides,然后深入讲解guides:提供者设置、使用hooks渲染以及调试。
Overview
概述
The skill is organized into four focused rule files. Client-framework guidance is scoped per framework via a suffix (currently only React). Cross-framework concepts live in unsuffixed files.
-<framework>- Feeds vs. guides (framework-agnostic) — which product to pick for a given surface and why
- Setting up the guide providers in React — and
KnockProviderprops, where each value comes from, and how to sequence themKnockGuideProvider - Rendering guides in React — building a guide component with /
useGuide, typed content, and engagement trackinguseGuides - Debugging guides (framework-agnostic) — the guides toolbar, the triage checklist, and testing workflow
Framework scope: right now this skill only covers React (). If the user is building with Vue, Svelte, plain JS, React Native, iOS, or Android, stop and ask how they'd like to proceed — do not adapt the React rules to another client SDK on your own.@knocklabs/react
本技能指南分为四个聚焦规则文件。客户端框架相关指南通过后缀区分(目前仅支持React)。跨框架概念存放在无后缀的文件中。
-<framework>- Feeds vs. guides(跨框架通用)——针对特定场景选择合适产品的原因
- 在React中设置引导组件提供者——和
KnockProvider的属性、各属性值的来源以及它们的加载顺序KnockGuideProvider - 在React中渲染引导组件——使用/
useGuide构建引导组件、类型化内容以及互动追踪useGuides - 调试引导组件(跨框架通用)——引导组件工具栏、分类检查清单和测试流程
**框架范围说明:**目前本技能指南仅覆盖React()。如果用户使用Vue、Svelte、原生JS、React Native、iOS或Android进行开发,请暂停并询问用户的下一步需求——请勿自行将React规则适配到其他客户端SDK。@knocklabs/react
How to use this skill
如何使用本技能指南
When deciding what to build
决定构建内容时
Start with :
rules/feeds-vs-guides.md- Confirm the surface you're building is actually a guide, not a feed
- Check the decision table before picking a direction
- If the answer is "both," wrap the app in once and render each product's provider where it's needed
KnockProvider
从开始:
rules/feeds-vs-guides.md- 确认你要构建的场景确实是guide而非feed
- 在选择方向前查看决策表
- 如果需要同时使用两者,只需在应用中包裹一次,并在需要的地方渲染对应产品的提供者
KnockProvider
When adding guides to a React app for the first time
首次在React应用中添加引导组件时
- Read
rules/setup-guide-providers-react.md - Before running any CLI commands, confirm the CLI is authenticated and which Knock environment this setup is for. First run — if it errors with something like "not authenticated" or "no user session," run
knock whoamiand ask the user to complete the browser flow before continuing (the CLI persists the session so this is a one-time step per machine). Only afterknock loginsucceeds, runknock whoamiand ask the user to pick (the CLI defaults toknock environment list, but most real integrations targetdevelopment). Remember that slug asproductionand pass<env-slug>on every subsequent environment-scoped--environment <env-slug>command.knock - Before asking the user anything about the channel, discover via the Knock CLI: run
channelId. Channels are account-scoped, so this command does not takeknock channel list --json | jq -r '.[] | select(.key == "knock-guide") | .id'. If it prints a UUID, use it — do not ask the user to confirm or re-paste. Only ask the user if the CLI returns nothing or errors. See the rule file's "Where to get--environment" procedure for the full fallback order.channelId - Ask the user only for values that can't be auto-discovered — primarily the public for the chosen environment (and confirm
apiKeyis coming from the app's auth context). Do not bundle theuser.idask withapiKey.channelId - Wire +
KnockProviderat the top of the tree.KnockGuideProvider - Gate on any async data your targeting depends on.
readyToTarget - Get a real guide rendering before stopping. Run , show the user the options (
knock guide list --environment <env-slug> --json,key, each step'sname), and build the first component against a real guide's actual values. Do not scaffold with placeholder strings likeschema_key. Fetch the message type schema with"changelog-card"so the content is typed. If the environment has no guides, offer to scaffold a test one via the Knock CLI (knock message-type get <schema_key> --environment <env-slug> --json→ edit the JSON →knock guide new) using a built-in message type (knock guide push --environment <env-slug>,card, orbanner) with obvious-placeholder content — don't stall waiting for manual dashboard setup. Seemodal→ "First guide: discover real guides via CLI before writing code" for the full procedure including the empty-environment branch.rules/rendering-guides-react.md - Flag anything that still needs the user (paste key, flip the guide to active in the dashboard, restart dev server) explicitly — don't leave them to discover it by absence.
pk_
- 阅读
rules/setup-guide-providers-react.md - **在运行任何CLI命令前,确认CLI已认证以及本次设置对应的Knock环境。**先运行——如果出现“未认证”或“无用户会话”之类的错误,运行
knock whoami并等待用户完成浏览器端流程后再继续(CLI会持久化会话,因此这是每台机器仅需执行一次的步骤)。只有在knock login执行成功后,再运行knock whoami并让用户选择目标环境(CLI默认是knock environment list,但大多数实际集成会针对development)。记住该环境的标识为production,并在后续所有与环境相关的<env-slug>命令中添加knock参数。--environment <env-slug> - 在询问用户任何关于渠道的信息前,通过Knock CLI获取:运行
channelId。渠道是账号级别的,因此该命令不需要knock channel list --json | jq -r '.[] | select(.key == "knock-guide") | .id'参数。如果输出UUID,直接使用——无需让用户确认或重新粘贴。只有当CLI无输出或报错时,才询问用户。完整的 fallback 流程请查看规则文件中的“获取--environment的位置”部分。channelId - 仅向用户询问无法自动获取的值——主要是所选环境的公开(并确认
apiKey来自应用的认证上下文)。不要将user.id和apiKey的询问合并。channelId - 在组件树的顶层配置+
KnockProvider。KnockGuideProvider - 将依赖于你的目标规则所需的任何异步数据。
readyToTarget - **确保成功渲染一个真实的引导组件后再停止。**运行,向用户展示可选的引导组件(
knock guide list --environment <env-slug> --json、key、每个步骤的name),并根据真实引导组件的实际值构建第一个组件。不要使用schema_key之类的占位符字符串。使用"changelog-card"获取消息类型 schema,以便内容类型化。如果环境中没有引导组件,可通过Knock CLI搭建一个测试用引导组件(knock message-type get <schema_key> --environment <env-slug> --json→ 编辑JSON →knock guide new),使用内置消息类型(knock guide push --environment <env-slug>、card或banner)并添加明显的占位符内容——不要等待手动在仪表盘设置而停滞。完整流程包括空环境分支,请查看modal→ “第一个引导组件:编写代码前通过CLI发现真实引导组件”部分。rules/rendering-guides-react.md - 明确标记仍需用户完成的事项(粘贴开头的密钥、在仪表盘将引导组件切换为激活状态、重启开发服务器)——不要让用户自行发现这些遗漏的步骤。
pk_
When building a new guide component in React
在React中构建新的引导组件时
- Follow the workflow in
rules/rendering-guides-react.md - Discover the target guide (or message type) via the Knock CLI before picking values. for the guide's
knock guide list --environment <env-slug> --json/ stepkey;schema_keyfor the content schema. Avoid placeholder strings.knock message-type get <schema_key> --environment <env-slug> --json - Pick for single-guide surfaces,
useGuidefor listsuseGuides - Define a TypeScript type that mirrors the message type schema you just pulled
- Wire ,
markAsSeen, andmarkAsInteracted— custom components must do this themselvesmarkAsArchived
- 遵循中的流程
rules/rendering-guides-react.md - **在选择值前,通过Knock CLI发现目标引导组件(或消息类型)。**使用获取引导组件的
knock guide list --environment <env-slug> --json/步骤的key;使用schema_key获取内容schema。避免使用占位符字符串。knock message-type get <schema_key> --environment <env-slug> --json - 单个引导组件场景选择,列表场景选择
useGuideuseGuides - 定义一个与你获取到的消息类型schema对应的TypeScript类型
- 配置、
markAsSeen和markAsInteracted——自定义组件必须自行实现这些功能markAsArchived
When a guide isn't rendering
引导组件无法渲染时
- Open and work the triage checklist top to bottom
rules/debugging-guides.md - Turn on the guides toolbar () first — it answers most questions in seconds
?knock_guide_toolbar=true - Distinguish server-side (targeting/eligibility) from client-side (provider/component) failures before digging deeper
- 打开,从上到下执行分类检查清单
rules/debugging-guides.md - 首先开启引导组件工具栏()——它能在几秒内解答大多数问题
?knock_guide_toolbar=true - 在深入排查前,先区分服务端(目标定位/资格验证)和客户端(提供者/组件)的故障
Rule files reference
规则文件参考
- — product selection between feeds and guides (framework-agnostic)
rules/feeds-vs-guides.md - — configuring
rules/setup-guide-providers-react.mdandKnockProviderfor guides (React)KnockGuideProvider - —
rules/rendering-guides-react.md,useGuide, typed content, engagement tracking (React)useGuides - — toolbar, triage checklist, testing workflow (framework-agnostic)
rules/debugging-guides.md
- ——feeds和guides之间的产品选择(跨框架通用)
rules/feeds-vs-guides.md - ——为guides配置
rules/setup-guide-providers-react.md和KnockProvider(React)KnockGuideProvider - ——
rules/rendering-guides-react.md、useGuide、类型化内容、互动追踪(React)useGuides - ——工具栏、分类检查清单、测试流程(跨框架通用)
rules/debugging-guides.md
Quick reference
快速参考
The examples below are React. For any other client SDK, see the note at the top of Overview before proceeding.
以下示例均为React。对于其他客户端SDK,请先查看概述顶部的说明再继续。
Providers (minimum viable setup — React)
提供者(最简可行配置——React)
tsx
<KnockProvider
apiKey={process.env.NEXT_PUBLIC_KNOCK_API_KEY}
user={{ id: currentUser.id }}
>
<KnockGuideProvider
channelId={process.env.NEXT_PUBLIC_KNOCK_GUIDE_CHANNEL_ID}
readyToTarget
listenForUpdates
>
{children}
</KnockGuideProvider>
</KnockProvider>tsx
<KnockProvider
apiKey={process.env.NEXT_PUBLIC_KNOCK_API_KEY}
user={{ id: currentUser.id }}
>
<KnockGuideProvider
channelId={process.env.NEXT_PUBLIC_KNOCK_GUIDE_CHANNEL_ID}
readyToTarget
listenForUpdates
>
{children}
</KnockGuideProvider>
</KnockProvider>Where to source each value
各值的来源
-
Auth first, then environment — Knock is environment-scoped. Before any CLI command, verify the CLI is authenticated with; if it errors, run
knock whoamiand wait for the user to complete the browser flow. Then runknock loginand confirm the target slug (knock environment list,production, …) with the user. Passdevelopmenton every subsequent environment-scoped--environment <env-slug>command. Don't rely on the CLI'sknockdefault.development -
— Knock dashboard → Platform → API keys → public
apiKeykey from the tab for the chosen environment (switch envs via the dashboard's environment selector first; remind the user to copy the key for the right env)pk_... -
— your auth context; must match the id used when identifying the user from your backend
user.id -
— the UUID of the guide channel, not its key. Channels are account-scoped, so
channelIddoes not takeknock channel list. Always attempt CLI discovery before asking the user:--environmentbashknock channel list --json | jq -r '.[] | select(.key == "knock-guide") | .id'If this prints a UUID, use it directly — don't prompt for confirmation. The default guide channel key is(typeknock-guide). Fall back to the dashboard (Settings → Integrations → Channels) only if the CLI returns nothing or errors. Seein_app_guidefor the full procedure.rules/setup-guide-providers-react.md
-
先认证,再选环境——Knock是按环境划分的。在执行任何CLI命令前,使用验证CLI已认证;如果报错,运行
knock whoami并等待用户完成浏览器端流程。然后运行knock login并与用户确认目标环境标识(knock environment list、production等)。在后续所有与环境相关的development命令中添加knock参数。不要依赖CLI默认的--environment <env-slug>环境。development -
——Knock仪表盘 → Platform → API keys → 所选环境标签页下的公开
apiKey密钥(先通过仪表盘的环境选择器切换到目标环境;提醒用户复制对应环境的密钥)pk_... -
——你的应用认证上下文;必须与后端识别用户时使用的ID一致
user.id -
——guide渠道的UUID,而非其key。渠道是账号级别的,因此
channelId命令不需要knock channel list参数。请始终先尝试通过CLI获取,再询问用户:--environmentbashknock channel list --json | jq -r '.[] | select(.key == "knock-guide") | .id'如果输出UUID,直接使用——无需提示用户确认。默认的guide渠道key是(类型为knock-guide)。只有当CLI无输出或报错时,才 fallback 到仪表盘(Settings → Integrations → Channels)。完整流程请查看in_app_guide。rules/setup-guide-providers-react.md
Hooks at a glance
Hooks概览
- — one guide by message type
useGuide({ type }) - — one specific guide by key
useGuide({ key }) - — array of guides by message type
useGuides({ type }) - — low-level client access
useGuideContext()
- ——按消息类型获取单个引导组件
useGuide({ type }) - ——按key获取特定的单个引导组件
useGuide({ key }) - ——按消息类型获取引导组件数组
useGuides({ type }) - ——底层客户端访问
useGuideContext()
Engagement methods
互动方法
- — impression (call from
step.markAsSeen()keyed onuseEffect)step - — primary action
step.markAsInteracted() - — dismissal; removes the guide for this user going forward
step.markAsArchived()
- ——记录曝光(在依赖
step.markAsSeen()的step中调用)useEffect - ——记录主要操作
step.markAsInteracted() - ——记录关闭;从此为该用户移除该引导组件
step.markAsArchived()
First stop when something's wrong
故障排查第一步骤
Append to any URL. The toolbar shows all guides, which are active, which this user is eligible for, and why the rest were filtered out.
?knock_guide_toolbar=true在任意URL后追加。工具栏会显示所有引导组件、哪些处于激活状态、当前用户符合哪些引导组件的条件,以及其余引导组件被过滤的原因。
?knock_guide_toolbar=trueBest practices summary
最佳实践总结
- Pick the right product. Feeds for chronological lists, guides for targeted UI.
- Mount providers once, high in the tree. Inside your auth boundary, above any route that renders guides.
- Never pass a placeholder user. Wait for auth to resolve before mounting .
KnockProvider - Gate on async data your targeting rules depend on.
readyToTarget - Type your content. should mirror the Knock message type schema.
useGuide<T> - Always handle engagement. Custom components must call ,
markAsSeen, andmarkAsInteractedthemselves.markAsArchived - Use the toolbar first. Most "the guide isn't showing" questions are answered in seconds.
- 选择合适的产品——feeds用于时序列表,guides用于目标化UI。
- 在组件树顶层挂载一次提供者——在认证边界内,在所有渲染引导组件的路由之上。
- 绝不传递占位符用户——等待认证完成后再挂载。
KnockProvider - 将依赖于异步数据——你的目标规则所需的异步数据。
readyToTarget - 内容类型化——应与Knock消息类型schema保持一致。
useGuide<T> - 始终处理互动事件——自定义组件必须自行调用、
markAsSeen和markAsInteracted。markAsArchived - 优先使用工具栏——大多数“引导组件不显示”的问题都能在几秒内得到解答。