为什么你需要@reactions/component?5个场景解决React类组件写不动的痛点
为什么你需要@reactions/component?5个场景解决React类组件写不动的痛点
@reactions/component 是一个"声明式版本的 React.Component":它把 React 类组件的生命周期和状态管理,变成一个个可以直接写在 JSX 里的 props。你只需要生命周期或一点状态,却不想为一个计数器、一次数据请求专门新建一个类组件?它就是为这个痛点而生的,而且行为片段可以自由组合、嵌套复用。
一分钟理解:组件即函数式 props
传统写法下,只要你想在挂载时发请求、在卸载时清定时器,就得写一个完整的 class 组件:继承 React.Component、定义 state、实现 componentDidMount……
而 @reactions/component 的思路是:把生命周期"参数化"。核心实现就在 src/index.js,一个不足 60 行的类:
didMount/didUpdate/willUnmount/shouldUpdate都是 props,传函数进去即可initialState直接初始化 state- 渲染逻辑用 children 函数(render props)书写
所有钩子函数收到的参数统一为 { state, props, setState, forceUpdate },逻辑集中在 src/index.js 的 getArgs() 方法中。
快速安装:一条命令即可上手
npm install @reactions/component
# 或
yarn add @reactions/component
然后在代码中导入:
import Component from "@reactions/component";
也可以直接通过 <script> 标签引入全局变量 ReactionsComponent,适合快速原型验证,详见 README.md。
💡 该包基于 rollup 构建,支持 ES module、CommonJS 和 UMD 三种方式引入,配置见 rollup.config.js。
场景一:拉取异步数据,不用新建组件
痛点:页面里一段列表要请求接口,你只需要"挂载时 fetch 一次 + setState",但类组件要写几十行模板代码。
用 <Component> 包住需要数据的那一小块 JSX,把 didMount 作为 prop 传入即可:
<Component
initialState={{ gists: null }}
didMount={({ setState }) => {
fetch("https://api.github.com/gists")
.then(res => res.json())
.then(gists => setState({ gists }));
}}
>
{({ state }) =>
state.gists ? (
<ul>
{state.gists.map(gist => (
<li key={gist.id}>{gist.description}</li>
))}
</ul>
) : (
<div>Loading...</div>
)
}
</Component>
挂载逻辑在 src/index.js 中由 componentDidMount 转发给你的 didMount 函数,加载态与数据态都在 children 里声明式地表达,完全不用建类。
场景二:只需要一点状态,不写整个类组件
痛点:一个计数器、一个展开/收起开关,为了一个 count 就新建一个组件文件,太重了。
initialState + children 函数即可:
<Component initialState={{ count: 0 }}>
{({ setState, state }) => (
<div>
<button onClick={() => setState(state => ({ count: state.count - 1 }))}>-</button>
<span> {state.count} </span>
<button onClick={() => setState(state => ({ count: state.count + 1 }))}>+</button>
</div>
)}
</Component>
setState 支持函数式更新(基于 src/index.js 对 this.setState 的透传),行为与类组件里的 this.setState 完全一致,只是"住"在了 JSX 里。
场景三:把可复用的生命周期逻辑封装成"行为"
痛点:多个页面都要"挂载时订阅事件、卸载时清理",每个类组件里都复制一遍,改一处漏一处。
把生命周期逻辑抽出来,写成一个只包裹 <Component> 的封装组件:
function useEvent({ eventName, handler }) {
return ({ children }) => (
<Component
didMount={() => window.addEventListener(eventName, handler)}
willUnmount={() => window.removeEventListener(eventName, handler)}
>
{children}
</Component>
);
}
willUnmount 的实现在 src/index.js,卸载时自动调用,彻底告别"忘了 removeEventListener"的内存泄漏。以后任何地方想加这段行为,包一层即可,不用改宿主组件一行代码。
场景四:用 shouldUpdate 精细控制更新时机
痛点:父组件一刷新,无关子树跟着重渲染,卡顿难定位;想控制渲染又不想写 shouldComponentUpdate 类方法。
shouldUpdate prop 返回 true/false 即可拦截更新,底层逻辑见 src/index.js:
<Component
shouldUpdate={({ props, nextProps }) =>
props.userId === nextProps.userId
}
>
{({ state, props }) => <UserPanel user={props.userId} />}
</Component>
同理,didUpdate 还会额外收到 prevProps / prevState(src/index.js),方便做"diff 之后做副作用"这类场景。
场景五:自由组合、无限嵌套——它天生可组合
官方一句话形容它:"composable as heck"(组合性拉满)。因为每个 <Component> 内部又是一个完整的 React 组件,所以你可以:
- 纵向嵌套:外层管数据、内层管交互,职责分层清晰
- 横向拆分:把"订阅"、"曝光上报"、"滚动监听"各自包成独立的
<Component>,需要哪层就叠哪层 - 行为迁移:Hooks 出现前,它是 React 里最接近"hook"的声明式方案;即使在 Hooks 时代,给老类组件库做无侵入改造时它依然好用
API 一览:全部 props 速查表
所有 props 都只接收函数,参数签名在 README.md 中完整列出:
| Prop | 对应生命周期 | 额外参数 |
|---|---|---|
initialState |
初始化 state | — |
didMount |
挂载后 | state, setState, props, forceUpdate |
shouldUpdate |
更新前判断 | 另有 nextProps, nextState |
didUpdate |
更新后 | 另有 prevProps, prevState |
willUnmount |
卸载前 | state, props |
children / render |
渲染逻辑 | 二选一,children 优先 |
渲染分支的优先级(children 优先于 render)见 src/index.js。
注意事项:项目现状与版本
- 该仓库由 Ryan Florence 维护,发布在 MIT 协议下(见 LICENSE)
- README 开头注明:项目已演进至 Reach UI,此仓库作为历史版本保留,核心版本为 2.0.2(见 package.json),peer 依赖为 React 15.x / 16.x
- 官方演示页面保留了可直接交互的计数器与数据加载 Demo,可查看 index.html
总结
@reactions/component 解决的不是"能不能写类组件",而是"没必要写那么重"。5 个典型场景——异步数据、轻量状态、行为封装、渲染控制、自由组合——覆盖了类组件最繁琐的日常。它用不足 60 行源码(src/index.js)证明:生命周期和状态可以像函数一样传递、组合,这正是后来 Hooks 思想的先声。
下次再想新建一个"只是拉个数据、管个开关"的类组件时,先问问自己:一个 <Component> 是不是就够了?