@fluentui-react-native/use-slot

中文文档
0.8.1发布于 2026/06/26MITTypeScriptES Module近 30 天下载 2150

Hook function to use a component as a pluggable slot

原始使用文档
文档目录5 节
  1. Looking a bit more deeply at rendering JSX
  2. Staged Components
  3. stagedComponent
  4. useSlot
  5. withSlots

@fluentui-react-native/use-slot

A pattern and framework for building layering components together in an efficient, pluggable, and customizable manner.

When building libraries of components, larger and more complex components are often built out of smaller and more focused components. This is great conceptually because it allows good separation of concerns and enables better and more modular designs. The boundaries in this layering are not only defined by the definition of your component, whether it be a function or a class based component, but by React.createElement. The createElement calls are what JSX is syntactic sugar for, and provides the layering for things like hooks.

Looking a bit more deeply at rendering JSX

When the render tree is returned via <View> etc, these calls are turned into calls to React.createElement

  • This creates an entry in the virtual DOM
  • This entry has benefits such as allowing render passes to be initiated at that node
  • It also effectively creates storage for hooks. Hooks are stored almost like stack frames for each component layer. This is why hooks cannot be conditional from render to render, it would cause stack misalignment.

If instead a function based component is rendered via a direct function call, e.g. return Text(textProps) this entry will not be created.

  • This does not create a DOM entry, which saves overhead in the overall react tree
  • This is only safe if the component does not use hooks and is a function component (not a class based one)

There are pros and cons to calling createElement. In most cases it is safer to do so. But with certain component patterns, particularly simple wrapping, this creates unnecessary overhead and as HOCs are built up this can become substantial. The patterns in this package attempt to alleviate this by allowing components to be authored in a way that they can be compressed safely.

Staged Components

To solve this reliably for function components it is necessary to separate the hook calls from element tree generation. The pattern in this package is to write render functions that return a continuation function that will return the JSX tree. This pattern is as follows:

const twoPartRender = (props: MyProps) => {
  // do the hook calls in this section */
  const theme = React.useContext(ThemeContext);
  const [state, setState] = React.useState(() => doSomeStateSetup()));

  // now return a (props) or (props, children) or (props, ...children) function that finishes the render
  return (additional: MyProps, ...children: React.ReactNode[]) => {
    const merged = { ...props, ...additional };
    return (<View {...merged}>{children}</View>)
  }
}

stagedComponent

This type of function is not recognizable on its own as a component. This package exports a helper function stagedComponent that both:

  • Turns a two part render function into a component that react can recognize
  • Adds the means for the framework to access the initial two part function if it knows how to handle it

The helper will return a React.FunctionComponent that will forward props (without children) to the first call, then pass children to the continuation function.

useSlot

Consuming these by hand are a bit tedious. To aid in this the useSlot hook function is provided. Besides enabling automatic tree compression (for component types that support it), this also allows components to be authored as pluggable bits in the render tree. Usage looks something like:

/** @jsxRuntime classic */
/** @jsx withSlots */

const baseStyle = {
  /* some text styling defaults set here */
};

// prop type that adds the ability to swap out the internal Text for something else
type TextWithOverrideProps = TextProps & { override?: React.FunctionComponent<TextProps> };

const StyledText = (props: React.PropsWithChildren<TextWithOverrideProps>) => {
  // split the children from props to forward them
  const { children, override, ...rest } = props;

  // create a merged set of props. The mergeStyle utility here will avoid creating unnecessary permutations of styles
  const mergedProps = { ...rest, style: mergeStyles<TextStyle>(baseStyle, rest.style) };

  // create a slot that can be used to render, props passed in here will be remembered in render. If override is set the slot will be changed, otherwise Text will be used
  const InnerText = useSlot(override || Text, mergedProps);

  // now just render using that slot
  return <InnerText>{children}</InnerText>;
};

withSlots

The withSlots helper is required to render the slots correctly. The @jsx directive in the previous example is all that is required to make this work. This provides a helper to the @jsx processing utility that will render as a function if possible, or via createElement if not.

版本与发布记录

@fluentui-react-native/use-slot 共发布 42 个版本,首次发布于 2021/07/27;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.8.1latest2026/06/264 个月前
0.8.02026/06/184 个月前
0.7.32026/04/256 个月前
0.7.12026/03/048 个月前
0.7.02026/02/258 个月前
0.6.122025/08/061.3 年前

关于 @fluentui-react-native/use-slot 的常见问题

@fluentui-react-native/use-slot 是什么?

Hook function to use a component as a pluggable slot

如何安装 @fluentui-react-native/use-slot?

在项目目录运行 npm install @fluentui-react-native/use-slot,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(18.2.0 || 19.0.0 || 19.1.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fluentui-react-native/use-slot 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluentui-react-native/use-slot 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fluentui-react-native/use-slot 可以商用吗?

@fluentui-react-native/use-slot 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fluentui-react-native/use-slot 的下载量有多少?

根据 npm 官方下载统计,@fluentui-react-native/use-slot 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,150 次,日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluentui-react-native/use-slot 还在维护吗?

最新版本 0.8.1 发布于 2026/06/26(4 个月前);至今共发布 42 个版本,其中过去 12 个月发布了 5 个;首次发布于 2021/07/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。