@grapesjs/react

中文文档
2.0.0发布于 2025/02/20MITTypeScriptESM + CJS近 30 天下载 36.1万
原始使用文档
文档目录7 节
  1. Installation
  2. Compatibility
  3. Default UI
  4. Custom UI
  5. Get the editor in your components
  6. Development
  7. License

GrapesJS React

Requires GrapesJS v0.21.3 or higher

The official GrapesJS wrapper for React that allows you to build custom and declarative UI for your editor.

The goal of this library is not to provide UI components but simple wrappers around the core GrapesJS modules and let you define your own UI components and access easily the GrapesJS API.

Warning: This library is NOT intended to render your React components inside the GrapesJS canvas, here it's all about the custom UI around the canvas itself.

Installation

The core grapesjs library is not bundled with the wrapper itself so you have to install it separately.

npm i grapesjs @grapesjs/react

Compatibility

@grapesjs/react v1.X

  • React v18.X
  • GrapesJS v0.21.3 or higher
  • NextJS v14.X

@grapesjs/react v2.X

  • React v18.X or v19.X
  • GrapesJS v0.22.5 or higher
  • NextJS v15.X

Default UI

This is the simplest and more traditional way to use the wrapper with GrapesJS as it relies on the default UI provided by GrapesJS. This approach is less customizable from React perspective and all the UI customization has to be done via GrapesJS (basically how you would do without the wrapper).

import grapesjs, { Editor } from 'grapesjs';
import GjsEditor from '@grapesjs/react';

export default function DefaultEditor() {
  const onEditor = (editor: Editor) => {
    console.log('Editor loaded', { editor });
  };

  return (
    <GjsEditor
      // Pass the core GrapesJS library to the wrapper (required).
      // You can also pass the CDN url (eg. "https://unpkg.com/grapesjs")
      grapesjs={grapesjs}
      // Load the GrapesJS CSS file asynchronously from URL.
      // This is an optional prop, you can always import the CSS directly in your JS if you wish.
      grapesjsCss="https://unpkg.com/grapesjs/dist/css/grapes.min.css"
      // GrapesJS init options
      options={{
        height: '100vh',
        storageManager: false,
      }}
      onEditor={onEditor}
    />
  );
}

Live Example

Custom UI

This is the more powerful and declarative way to use the wrapper as it gives you full control over the UI of your editor. Using the <Canvas/> component inside the main wrapper will disable the core default UI.

import grapesjs, { Editor } from 'grapesjs';
import GjsEditor, { Canvas } from '@grapesjs/react';

export default function CustomEditor() {
  const onEditor = (editor: Editor) => {
    console.log('Editor loaded', { editor });
  };

  return (
    <GjsEditor
      grapesjs={grapesjs}
      grapesjsCss="https://unpkg.com/grapesjs/dist/css/grapes.min.css"
      onEditor={onEditor}
      options={{
        height: '100vh',
        storageManager: false,
      }}
    >
        <div>
        // ... use your UI components
            <Canvas /> // place the GrapesJS canvas where you wish
        // ...
        </div>
    </GjsEditor>
  );
}

Here below you can find a good example (with the usage of all available providers) of how you would build a full custom editor around GrapesJS by using your own React components.

Note: The app doesn't cover all the GrapesJS API but simply provides a good starting point to understand how to create your own custom editor.

Live Example

Get the editor in your components

You can easily access the editor instance in your custom components by using the useEditor hook. One only caveat is to use it once the editor is actually created and for this use case, you can rely on the <WithEditor> component as a wrapper. Alternatively, you can use useEditorMaybe hook and check by yourself if the editor exists.

import GjsEditor, { WithEditor, useEditor, useEditorMaybe } from '@grapesjs/react';

function MyComponentWithUseEditor() {
    // The `editor` is always defined.
    const editor = useEditor();
    return (...);
}

function MyComponentWithUseEditorMaybe() {
    // The `editor` is not immediately available
    const editor = useEditorMaybe();
    return (
        <div>
            <div>I will be rendered immediately</div>
            <div>
                Editor: { editor ? 'created' : 'not yet created' }.
            </div>
        </div>
    );
}

export default function CustomEditor() {
  return (
    <GjsEditor ...>
        // ...
        <WithEditor>
            // This component won't be rendered until the editor is created
            <MyComponentWithUseEditor/>
        </WithEditor>
        <MyComponentWithUseEditorMaybe/>
    </GjsEditor>
  );
}

By using components inside the built-in providers, the editor is always defined.

Development

Clone the repository

$ git clone https://github.com/GrapesJS/react.git
$ cd react

Install it

$ yarn i

Start the dev server

$ yarn start

License

MIT

版本与发布记录

@grapesjs/react 共发布 4 个版本,首次发布于 2023/07/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2025/02/201.7 年前
2.0.0-rc.4rc2025/02/141.8 年前
1.0.02023/07/053 年前
0.1.02023/07/043 年前

关于 @grapesjs/react 的常见问题

如何安装 @grapesjs/react?

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

@grapesjs/react 支持 TypeScript 吗?

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

@grapesjs/react 支持 ESM 还是 CommonJS?

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

@grapesjs/react 可以商用吗?

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

@grapesjs/react 的下载量有多少?

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

@grapesjs/react 还在维护吗?

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