@basemachina/react-inspector

中文文档
8.1.1发布于 2025/10/03MITTypeScriptESM + CJS零依赖近 30 天下载 5151

Power of Browser DevTools inspectors right inside your React app

原始使用文档
文档目录12 节
  1. Changes
  2. Install
  3. Getting started
  4. <Inspector />
  5. <ObjectInspector />
  6. <TableInspector />
  7. <DOMInspector />
  8. More Examples
  9. Theme
  10. Roadmap
  11. Contribution
  12. Additional

react-inspector

This repository is a fork of storybookjs/react-inspector.

Changes

Here are the main changes:

  • support replacer prop to replace the object properties like replacer argument in JSON.stringify

Power of Browser DevTools inspectors right inside your React app. Check out the interactive playground or storybook.

''

''

''

Install

NPM:

npm install @basemachina/react-inspector

Recommended versions:

  • version 3.0.2: If you are using React 16.8.4 or later.
  • version 2.3.1: If you are using an earlier version of React.

Getting started

<Inspector />

A shorthand for the inspectors.

  • <Inspector/> is equivalent to <ObjectInspector> or <DOMInspector> if inspecting a DOM Node.
  • <Inspector table/> is equivalent to <TableInspector>.

<ObjectInspector />

Like console.log. Consider this as a glorified version of <pre>JSON.stringify(data, null, 2)</pre>.

How it works

Tree state is saved at root. If you click to expand some elements in the hierarchy, the state will be preserved after the element is unmounted.

API

The component accepts the following props:

data: PropTypes.any: the Javascript object you would like to inspect

name: PropTypes.string: specify the optional name of the root node, default to undefined

expandLevel: PropTypes.number: an integer specifying to which level the tree should be initially expanded

expandPaths: PropTypes.oneOfType([PropTypes.string, PropTypes.array]): an array containing all the paths that should be expanded when the component is initialized, or a string of just one path

  • The path string is similar to JSONPath.
    • It is a dot separated string like $.foo.bar. $.foo.bar expands the path $.foo.bar where $ refers to the root node. Note that it only expands that single node (but not all its parents and the root node). Instead, you should use expandPaths={['$', '$.foo', '$.foo.bar']} to expand all the way to the $.foo.bar node.
    • You can refer to array index paths using ['$', '$.1']
    • You can use wildcard to expand all paths on a specific level
      • For example, to expand all first level and second level nodes, use ['$', '$.*'] (equivalent to expandLevel={2})
  • the results are merged with expandLevel

showNonenumerable: PropTypes.bool: show non-enumerable properties

sortObjectKeys: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]): Sort object keys with optional compare function

When sortObjectKeys={true} is provided, keys of objects are sorted in alphabetical order except for arrays.

nodeRenderer: PropTypes.func: Use a custom nodeRenderer to render the object properties (optional)

  • Instead of using the default nodeRenderer, you can provide a custom function for rendering object properties. The default nodeRender looks like this:

    import { ObjectRootLabel, ObjectLabel } from 'react-inspector'
    
    const defaultNodeRenderer = ({ depth, name, data, isNonenumerable, expanded }) =>
      depth === 0
        ? <ObjectRootLabel name={name} data={data} />
        : <ObjectLabel name={name} data={data} isNonenumerable={isNonenumerable} />;
    

replacer: PropTypes.func: Use a custom replacer to replace the object properties like replacer argument in JSON.stringify (optional)

import { ObjectInspector } from 'react-inspector'

const replacer = ({ data }) => {
  // replace the array data with the first 10 elements
  if (Array.isArray(data)) {
    return data.slice(0, 10);
  }
  return data;
}

const MyComponent = ({ data }) =>
  <div>
    <ObjectInspector data={data} replacer={replacer} />
  </div>

<TableInspector />

Like console.table.

API

The component accepts the following props:

data: PropTypes.oneOfType([PropTypes.array, PropTypes.object]): the Javascript object you would like to inspect, either an array or an object

columns: PropTypes.array: An array of the names of the columns you'd like to display in the table

<DOMInspector />

API

The component accepts the following props:

data: PropTypes.object: the DOM Node you would like to inspect

Usage

import { ObjectInspector, TableInspector } from 'react-inspector';

// or use the shorthand
import { Inspector } from 'react-inspector';

const MyComponent = ({ data }) =>
  <div>
    <ObjectInspector data={data} />
    <TableInspector data={data} />

    <Inspector data={data} />
    <Inspector table data={data} />
  </div>

let data = { /* ... */ };

ReactDOM.render(
  <MyComponent data={data} />,
  document.getElementById('root')
);

Try embedding the inspectors inside a component's render() method to provide a live view for its props/state (Works even better with hot reloading).

More Examples

Check out the storybook for more examples.

npm install && npm run storybook

Open http://localhost:9001/

Theme

By specifying the theme prop you can customize the inspectors. theme prop can be

  1. a string referring to a preset theme ("chromeLight" or "chromeDark", default to "chromeLight")
  2. or a custom object that provides the necessary variables. Checkout src/styles/themes for possible theme variables.

Example 1: Using a preset theme:

<Inspector theme="chromeDark" data={{a: 'a', b: 'b'}}/>

Example 2: changing the tree node indentation by inheriting the chrome light theme:

import { chromeLight } from 'react-inspector'

<Inspector theme={{...chromeLight, ...({ TREENODE_PADDING_LEFT: 20 })}} data={{a: 'a', b: 'b'}}/>

Roadmap

Type of inspectors:

  • Tree style
    • common objects
    • DOM nodes
  • Table style
    • Column resizer
  • Group style

Contribution

Contribution is welcome. Past contributors

Additional

  • If you intend to capture console.logs, you may want to look at console-feed.
  • react-object-inspector package will be deprecated. <ObjectInspector/> is now part of the new package react-inspector.
  • Why inline style? This document summarizes it well.

版本与发布记录

@basemachina/react-inspector 共发布 2 个版本,首次发布于 2025/10/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
8.1.1latest2025/10/031.1 年前
8.1.02025/10/031.1 年前

关于 @basemachina/react-inspector 的常见问题

@basemachina/react-inspector 是什么?

Power of Browser DevTools inspectors right inside your React app

如何安装 @basemachina/react-inspector?

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

@basemachina/react-inspector 支持 TypeScript 吗?

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

@basemachina/react-inspector 支持 ESM 还是 CommonJS?

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

@basemachina/react-inspector 可以商用吗?

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

@basemachina/react-inspector 的下载量有多少?

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

@basemachina/react-inspector 还在维护吗?

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