@reactions/component避坑与故障排查:React 15/16版本兼容、UMD全局变量与常见错误完整清单
@reactions/component避坑与故障排查:React 15/16版本兼容、UMD全局变量与常见错误完整清单
@reactions/component 是 React 生态中一个轻量级库,提供 React.Component 的声明式版本:不需要定义新的类组件,就能使用 state、setState 和全部生命周期回调。它只兼容 React 15.x / 16.x,且 UMD 构建依赖特定全局变量名,因此安装报错、ReactionsComponent is not defined 是最高频的两个故障。本文给出一份完整清单,按「版本兼容 → UMD 全局变量 → 错误对照表 → 使用细节」的顺序逐一排查,帮你快速定位问题。
一、30秒快速了解 @reactions/component
这个库的核心思想:把生命周期「声明」成 props 传进去,而不是写一个类。
| 文件 | 作用 |
|---|---|
| src/index.js | 全部源码,只有一个 Component 类 |
| package.json | 依赖声明,关键是 peerDependencies |
| rollup.config.js | UMD 构建配置,决定全局变量名 |
| README.md | 官方用法示例 |
| index.html | 可运行的演示页 |
⚠️ 项目状态提示:README.md 开头注明,项目已迁移到 Reach UI,本仓库保留用于存档。新启动的项目建议优先考虑替代方案,存量项目则继续按本清单排查。
二、React 15/16 版本兼容:安装即报错的排查步骤
打开 package.json#L15-L17,可以看到版本约束:
"peerDependencies": {
"react": "15.x || 16.x"
}
这意味着 React 17、18、19 全部不被官方支持。这是最常见的"还没写代码就报错"的场景:
故障现象 1:npm 7+ 安装直接失败
# 典型报错(核心片段)
ERESOLVE overriding peer dependency
Could not resolve dependency:
peer react@"15.x || 16.x" from @reactions/component
排查步骤:
- 运行
npm ls react确认当前 React 版本; - 若在 React 17+ 项目中必须引入,可选方案:
- 将 React 锁定在
16.x(最稳妥,与 yarn.lock 中开发环境的react@^16.3.0保持一致); - 或使用
npm install --legacy-peer-deps跳过 peer 校验(风险自担);
- 将 React 锁定在
- 若目标是新项目,直接改用 Hooks 或 Reach UI 的对应组件,不必引入本库。
三、UMD 全局变量:ReactionsComponent 的三个高频坑
不走打包器、直接用 <script> 引入时,有三处极易踩错:
坑 1:全局变量名不是 Component
UMD 构建的导出名定义在 rollup.config.js#L9-L14:
output: {
name: 'ReactionsComponent',
globals: { react: 'React' }
}
所以必须写成 const Component = ReactionsComponent;。写成 window.Component 或 window.Reactions 都会得到 undefined。
坑 2:必须先加载 React 和 ReactDOM
globals 配置声明了构建依赖外部全局 React。加载顺序必须是:react → react-dom → @reactions/component。顺序反了会报 ReactionsComponent is not defined 或 React 初始化异常。
坑 3:注意仓库里的历史遗留命名
演示页 index.html#L280-L283 及 templates/footer.html 中仍引用旧包名时代的全局变量 ReactComponentComponent(包改名前叫 react-component-component)。如果你照着旧文档或这份演示页取 ReactComponentComponent,拿到的是 undefined —— 请以 ReactionsComponent 为准。
四、常见错误完整清单(对照表)
| 错误 / 现象 | 原因 | 解决方案 |
|---|---|---|
ERESOLVE / UNMET PEER DEPENDENCY react |
React 版本不在 15.x / 16.x 范围 | 锁定 React 16,或加 --legacy-peer-deps |
ReactionsComponent is not defined |
UMD 脚本未加载,或加载顺序/全局名写错 | 先加载 React/ReactDOM,用 ReactionsComponent 取值 |
window.Component 为 undefined |
UMD 导出名是 ReactionsComponent |
见 rollup.config.js#L9-L14 |
React.createElement: type is invalid |
UMD 构建找不到全局 React,组件类未完成初始化 |
确认 react 脚本在组件脚本之前 |
| 页面空白、无报错 | children 传了非函数,被原样渲染 |
children 必须是 ({ state, setState, props }) => JSX |
回调里调用 this.setState 报 this is undefined |
回调参数里没有 this,要用传入的 setState |
见 src/index.js#L8-L16 |
卸载时报 Cannot read properties of undefined (setState) |
willUnmount 回调不含 setState |
见 src/index.js#L33-L39 |
五、源码级使用细节:容易忽略的 5 个约定
逐条对照 src/index.js 的实现,这些行为写文档时容易被省略:
initialState直接成为初始 state(src/index.js#L4):传入什么就是什么,不存在默认值兜底;children优先于render:children是函数时以它为准;不是函数则原样渲染(src/index.js#L51-L56);shouldUpdate返回false才跳过更新,不传时恒为true(src/index.js#L22-L31);didUpdate额外携带prevProps/prevState,可做前后对比(src/index.js#L41-L49);willUnmount只收到{ state, props },没有setState和forceUpdate(src/index.js#L33-L39)。
六、快速排查清单(照此执行)
- [ ]
npm ls react→ 版本是否为 15.x 或 16.x - [ ] 安装是否用了
--legacy-peer-deps(确认是有意为之) - [ ]
<script>顺序是否为react → react-dom → component - [ ] 全局变量是否取的
ReactionsComponent(而非旧名ReactComponentComponent) - [ ]
children/render回调是否为函数,且用参数里的setState - [ ] 是否误在
willUnmount中调用了setState
项目基于 MIT 协议发布(LICENSE),作者为 Ryan Florence。由于仓库已停止迭代并入 Reach UI,遇到本清单未覆盖的问题时,阅读 src/index.js 这 60 行源码是最快的排障路径。