@fluentui-react-native/use-tokens

中文文档
0.9.7发布于 2026/11/12MITTypeScriptES Module近 30 天下载 4127

Utilities and hook function for getting themed tokens for a component

原始使用文档
文档目录3 节
  1. Tokens
  2. Examples
  3. Layering

@fluentui-react-native/use-tokens

This package provides the means to create a hook function to build a set of tokens from a passed in theme as well as a cache object unique to the pairing of the given hook function and theme. These resolved tokens should then be used to build the styles for your component. This way the look and feel of a component can be built up from constants, theme globals, and even component specific overrides.

Tokens

A token is simply a setting that informs the styling of the component. These can be thought of as an additional set of component props that are automatically built-up by the framework. Typically these are set at component creation time or overriden by a theme but can can come from a variety of sources:

  • Objects - this is a set of values contained within an object. These values are assumed to be unchanging over the lifetime of the object.
  • Theme Functions - this allows values to be set in relation to values in a theme. An example would be a color being set to a particular entry in a color palette in the theme.
  • Name Lookup - this will query the theme for a token object or theme function for this component.

Examples

At the core of this system is the idea that: Tokens + Theme ==> Styles. This can be thought of as:

  • Your styles are produced via a recipe, ideally memoized via the returned cache or by the use of a useMemo hook or its equivalent
  • The tokens are the ingredients for your recipe
  • Some of those tokens come from constants
  • Some of those tokens come from the theme

The overall signature of the method is as follows:

export type StyledTextTokens = Pick<TextProps[style], 'color', 'fontFamily', 'fontWeight', 'fontSize'>;

const useTokens = buildUseTokens<StyledTextTokens, Theme>(
  /* function for looking up a component in a theme, allows this to be used independently of the shape of the theme */
  getComponentInfoFromTheme,

  /* Start of tokens, these are applied in order */
  /* object, these are effectively constants to be used as defaults */
  { token1: 'foo', token2: 400 },
  /* theme function */
  (theme: Theme) => ({ token3: theme.colors.backgroundColor }),
  /* name to lookup */
  'NameToLookup',
);

This useTokens hook can then be used to build styles for the component as follows:

const StyledText: React.FunctionComponent<TextProps> = (props) => {
  // split the props
  const { style, children, ...rest } = props;

  // left out of the hook to allow theme to be obtained in whatever way is appropriate. Generally from a context.
  const theme = useTheme();

  // get a set of tokens from the theme
  const [tokens, cache] = useTokens(theme);

  // create a style object from the tokens using the cache. Note that if all the tokens are actually directly styles the tokens could be used
  // directly. This is just to illustrate the pattern for a more complex component
  const [tokenStyle] = cache(() => ({ ...tokens }), []);

  // now merge any styles coming from the props in on top as they should override
  const mergedStyles = mergeStyles<TextStyle>(tokenStyle, style);

  // now just render the text element
  return (
    <Text {...rest} style={mergedStyles}>
      {children}
    </Text>
  );
};

Layering

These tokens will be deeply merged in the order they appear in the array.

const mergedTokens = merge(
  ...options.tokens.map(entry => {
    // string gets converted to object or function (or empty object)
    if (typeof entry === 'string') {
      entry = themeHelper.getComponentInfo(entry) || {};
    }
    // return either the function call (which will produce an object) or the object
    return typeof entry === 'function' ? entry(theme) : entry;
  });
);

版本与发布记录

@fluentui-react-native/use-tokens 共发布 46 个版本,首次发布于 2021/07/22;过去 12 个月发布了 12 个版本。

版本发布日期距今
0.9.7latest2026/11/124 天前
0.9.62026/10/2918 天前
0.9.52026/09/241 个月前
0.9.42026/09/231 个月前
0.9.32026/09/022 个月前
0.9.22026/08/212 个月前

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

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

Utilities and hook function for getting themed tokens for a component

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

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

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

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

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

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

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

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

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

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

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

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