如何快速上手@reactions/component:3步零类组件获得State与生命周期

原创2026-10-15 23:17:151,657 阅读

如何快速上手@reactions/component:3步零类组件获得State与生命周期

@reactions/component 是一个让 React 组件"零类化"的轻量工具包:不用写 class extends React.Component,只需一行 JSX 就能获得 State 管理和完整的生命周期钩子(didMount、didUpdate、willUnmount 等)。本文通过 3 个步骤带你快速上手这个声明式组件方案,并附带常用 Props 速查表。

🤔 为什么需要"零类"组件?

在 React 16 时代,只要你想在函数式组件之外拥有 state 或生命周期,就必须写一个完整的类组件:

  • 手动管理 state 初始化
  • 逐个挂载 componentDidMount、componentDidUpdate 等方法
  • 为"只是拿点异步数据"这种小需求新建整个组件文件,显得过重

@reactions/component 的思路很直接:把生命周期变成 props。你不需要定义新的类,只需在现有 JSX 中插入一个 <Component> 标签,声明式地声明"挂载时做什么、更新时做什么"。官方对它的描述是:Declarative version of React.Component(声明式版本的 React.Component)。

🚀 第 1 步:安装 @reactions/component

在终端执行以下命令之一:

npm install @reactions/component

或使用 yarn:

yarn add @reactions/component

⚠️ 注意:根据 package.json 中的 peerDependencies 声明,该库配套 React 15.x / 16.x 使用,在 React 17+(Hooks 时代)请谨慎评估。

📦 第 2 步:导入 Component

项目支持 ES Modules 和 CommonJS 两种导入方式:

// ES Modules
import Component from "@reactions/component";

// CommonJS
const Component = require("@reactions/component");

也支持直接用 <script> 标签引入,通过全局变量 ReactionsComponent 使用(UMD 构建配置见 rollup.config.js)。

🔢 第 3 步:用 JSX 声明 State 与生命周期

最经典的入门例子是一个计数器——整个交互逻辑写在同一个组件标签里,无需新建任何类:

<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>

代码拆解一下这 3 个关键部分:

部分 作用
initialState={{ count: 0 }} 初始化 State,等价于类组件里的 this.state
children 传一个函数 函数接收 { state, setState, props, forceUpdate },用于渲染
setState(state => ...) 支持函数式更新,与类组件的 setState 行为一致

这个例子完整来源于官方文档 README.md,可以直接照抄练习。

📋 生命周期 Props 速查表

<Component> 接受 6 个 props,每个都对应一个熟悉的生命周期或渲染入口:

Prop 对应生命周期 接收参数
didMount 挂载后(componentDidMount) { state, setState, props, forceUpdate }
shouldUpdate 更新前判断(shouldComponentUpdate) { state, props, nextProps, nextState }
didUpdate 更新后(componentDidUpdate) 额外含 prevProps、prevState
willUnmount 卸载前(componentWillUnmount) { state, props }
children 渲染入口(函数形式) { state, setState, props, forceUpdate }
render 渲染入口(与 children 二选一) { state, setState, props, forceUpdate }

💡 shouldUpdate 不设置时默认返回 true(始终更新),这一点在源码 src/index.js 中可以清楚看到。

🌐 实战场景:挂载时请求异步数据

"只想在挂载时 fetch 一次数据,不想为它建一个组件"——这正是该库的招牌场景:

<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>

didMount 回调拿到 setState 后直接发起请求,数据回来就更新状态触发重渲染——整个过程没有一个 class 关键字。生命周期转发的实现分别位于 src/index.js(didMount)与 src/index.js(willUnmount)。

🔍 原理一瞥:59 行源码看懂全部实现

这个库的核心全部集中在 src/index.js 中,本质上是一个"胶水类":

  1. State 注入:state = this.props.initialState,把 prop 直接赋为初始状态(src/index.js);
  2. 生命周期转发:componentDidMount 等方法内部判断对应 prop 是否存在,存在则把 { state, props, setState, forceUpdate } 作为参数调用(src/index.js 为 didUpdate 的转发逻辑);
  3. 渲染委托:render() 优先执行 children 函数,否则执行 render prop,否则返回 null(src/index.js)。

源码如此精简,意味着零额外心智负担——读一遍源码即可完成全部"学习成本"。

✅ 小结

问题 答案
它是什么? React.Component 的声明式版本,把生命周期变成 props
适合谁? 需要 State 或生命周期、但不想新建类组件的 React 15/16 开发者
核心 API initialState + didMount / didUpdate / willUnmount / shouldUpdate
主要文件 src/index.js、README.md

3 步回顾:安装 → 导入 → 用 JSX 声明 State 与生命周期。当你下次只想要一点点状态而整个组件显得"太重"时,@reactions/component 就是最轻量的选择。🎯

登录后查看全文
component