@bem-react/core

中文文档
6.1.1发布于 2026/08/18MPL-2.0TypeScriptESM + CJS近 30 天下载 3485

BEM React Core

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Use reexports for better DX
  4. Optimization. Lazy load for modifiers.
  5. Simple modifiers (only CSS classes)
  6. Debug

@bem-react/core · npm (scoped) npm bundle size (minified + gzip)

Core package helps organize and manage components with BEM modifiers in React.

Install

npm i -S @bem-react/core

Usage

Let's say, you have an initial App file structure as follows:

App.tsx
Components/
  Button/
    Button.tsx

And you need to set up two optional types of buttons that will be different from the Button.tsx. (In our example those will be Button of theme 'action' and Button of type 'link')

You can handle those using @bem-react/core.

Follow the guide.

Step 1.

In your Components/Button/index.tsx, you define the type of props your button can get within the interface that extends IClassNameProps from '@bem-react/core' :

import { ReactType } from 'react'
import { IClassNameProps } from '@bem-react/core'
import { cn } from '@bem-react/classname'

export interface IButtonProps extends IClassNameProps {
  as?: ReactType
}

export const cnButton = cn('Button')

Step 2.

Set up the basic Button variant which will be rendered if no modifiers props are set in the parent component. Inside your Components/Button/Button.tsx:

import React, { FC } from 'react'

import { IButtonProps, cnButton } from './index'

export const Button: FC<IButtonProps> = ({
  children,
  className,
  as: Component = 'button',
  ...props
}) => (
  <Component {...props} className={cnButton({}, [className])}>
    {children}
  </Component>
)

Step 3.

Set up the optional withButtonTypeLink and optional withButtonThemeAction variants that will be rendered if {type: 'link'} and/or {theme: 'action'} modifiers are set in the parent component respectively. Inside your Components/Button/ you add folders _type/ with Button_type_link.tsx file in it and _theme/ with Button_theme_action.tsx .

App.tsx
Components/
  Button/
    Button.tsx
    index.tsx
+   _type/
+     Button_type_link.tsx
+   _theme/
+     Button_theme_action.tsx

Set up the variants:

Note! The second parameter in withBemMod() is the condition for this component to be applied.

1. In Components/Button/_type/Button_type_link.tsx

import React from 'react'
import { withBemMod } from '@bem-react/core'

import { IButtonProps, cnButton } from '../index'

export interface IButtonTypeLinkProps {
  type?: 'link'
  href?: string
}

export const withButtonTypeLink = withBemMod<IButtonTypeLinkProps, IButtonProps>(
  cnButton(),
  { type: 'link' },
  (Button) => (props) => <Button {...props} as="a" />,
)

2. In Components/Button/_theme/Button_theme_action.tsx

import { withBemMod } from '@bem-react/core'

import { cnButton } from '../index'

export interface IButtonThemeActionProps {
  theme?: 'action'
}

export const withButtonThemeAction = withBemMod<IButtonThemeActionProps>(cnButton(), {
  theme: 'action',
})

Step 4.

Finally, in your App.tsx you need compose only necessary the variants with the basic Button. Be careful with the import order - it directly affects your CSS rules.

NOTE: If you wanna use same modifiers with different values you should use composeU for getting correctly typings.

import React, { FC } from 'react';
import { compose, composeU } from '@bem-react/core';

import { Button as ButtonPresenter } from './Components/Button/Button';
import { withButtonTypeLink } from './Components/Button/_type/Button_type_link';
import { withButtonThemeAction } from './Components/Button/_theme/Button_theme_action';
import { withButtonThemeDefault } from './Components/Button/_theme/Button_theme_default';

import './App.css';

const Button = compose(
  composeU(withButtonThemeAction, withButtonThemeDefault),
  withButtonTypeLink,
)(ButtonPresenter);

export const App: FC = () => (
  <div className="App">
    <Button>I'm basic</Button>
    // Renders into HTML as: <button class="Button">I'm Basic</button>

    <Button type="link" href="#stub">I'm type link</Button>
    // Renders into HTML as: <a href="#stub" class="Button Button_type_link">I'm type link</a>

    <Button theme="action">I'm theme action</Button>
    // Renders into HTML as: <button class="Button Button_theme_action">I'm theme action</button>

    <Button theme="action" type="link">I'm all together</Button>
    // Renders into HTML as: <a class="Button Button_theme_action Button_type_link">I'm all together</a>
  </div>
);

Note! The order of optional components composed onto ButtonPresenter is important: in case you have different layouts and need to apply several modifiers the FIRST one inside the compose method will be rendered! E.g., here:

export const Button = compose(
  withButtonThemeAction,
  withButtonTypeLink,
)(ButtonPresenter)

If your withButtonThemeAction was somewhat like

<button className={className}>{children}</button>

your JSX-component:

<Button type="link" theme="action">Hello</Button>

would render into HTML:

<a class="Button Button_theme_action Button_type_link">Hello</a>

Use reexports for better DX

IMPORTANT: use this solution if tree shaking enabled

Example:

Block/Block.tsx
Block/[email protected]
Block/_mod/Block_mod_val1.tsx
Block/_mod/Block_mod_val2.tsx
Block/_mod/Block_mod_val3.tsx

Create reexports for all modifiers in index files by platform: desktop, phone, amp, etc.

// Block/index.ts
export * from './Block'
export * from './Block/_mod'

// Block/desktop.ts
export * from './Block@desktop'
export * from './Block/_mod'

// Block/phone.ts
export * from './' // for feature if not created platform version

// Block/_mod/index.ts
export * from './Block_mod_val1.tsx'
export * from './Block_mod_val2.tsx'
export * from './Block_mod_val3.tsx'

Usage:

// App.tsx
import { Block as BlockPresenter, withModVal1 } from './components/Block/desktop'

const Block = withModVal1(BlockPresenter)

Optimization. Lazy load for modifiers.

Solution for better code splitting with React.lazy and dynamic imports

NOTE If your need SSR replace React.lazy method for load Block_mod.async.tsx module to @loadable/components or react-loadable

// Block/_mod/Block_mod.async.tsx
import React from 'react'
import { cnBlock } from '../Block'

import './Block_mod.css'

export const DynamicPart: React.FC = () => <i className={cnBlock('Inner')}>Loaded dynamicly</i>

// default export needed for React.lazy
export default DynamicPart
// Block/_mod/Block_mod.tsx
import React, { Suspense, lazy } from 'react'
import { cnBlock } from '../Block'

export interface BlockModProps {
  mod?: boolean
}

export const withMod = withBemMod<BlockModProps>(cnBlock(), { mod: true }, (Block) => (props) => {
  const DynamicPart = lazy(() => import('./Block_mod.async.tsx'))

  return (
    <Suspense fallback={<div>Updating...</div>}>
      <Block {...props}>
        <DynamicPart />
      </Block>
    </Suspense>
  )
})

Usage:

// App.tsx
import {
    Block as BlockPresenter,
    withMod
} from './components/Block/desktop';

const Block = withMod(BlockPresenter);

export const App = () => {
    return (
        {/* chunk with DynamicPart not loaded */}
        <Block />

        {/* chunk with DynamicPart loaded */}
        <Block mod />
    );
}

Simple modifiers (only CSS classes)

In most cases you need change only CSS class. This mode doesn't pass modififer value to props. It doesn't create React wrappers for component, that's way more optimal.

import React from 'react'
import { cnBlock } from '../Block'

export interface BlockModProps {
  simplemod?: boolean
}

export const withSimpleMod = createClassNameModifier<BlockModProps>(cnBlock(), { simplemod: true })

Debug

To help your debug "@bem-react/core" support development mode.

For <Button type="link" theme="action">Hello</Button> (from the Example above), React DevTools will show:

<WithBemMod(Button)[theme:action] ...>
  <WithBemMod(Button)[type:link] className="Button Button_theme_action" ...>
    <a className="Button Button_type_link Button_theme_action">Hello</a>
  </WithBemMod(Button)[type:link]>
</WithBemMod(Button)[theme:action]>

版本与发布记录

@bem-react/core 共发布 62 个版本,首次发布于 2018/08/29;过去 12 个月发布了 4 个版本。

版本发布日期距今
6.1.1latest2026/08/182 个月前
6.1.02026/02/278 个月前
6.0.1-dev.1next2026/02/258 个月前
6.0.02026/02/069 个月前
5.2.02025/06/151.4 年前
5.1.1-dev.42025/06/111.4 年前

其他发布通道:canary → 1.3.1,next → 6.0.1-dev.1。

关于 @bem-react/core 的常见问题

@bem-react/core 是什么?

BEM React Core

如何安装 @bem-react/core?

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

@bem-react/core 支持 TypeScript 吗?

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

@bem-react/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bem-react/core 可以商用吗?

@bem-react/core 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@bem-react/core 的下载量有多少?

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

@bem-react/core 还在维护吗?

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