项目作者: bramblex

项目描述 :
React Hooks API 在 16.6 上面的模拟实现
高级语言: TypeScript
项目地址: git://github.com/bramblex/react-hooks.git
创建时间: 2018-11-18T08:58:52Z
项目社区:https://github.com/bramblex/react-hooks

开源协议:

下载


react-hooks

React Hooks API 在低版本 React 16.6 下面的一个实现,可以在不升级 React 版本的前提下体验新的 React Hooks API。

Install

  1. npm install blx-react-hooks
  2. # or
  3. yarn add blx-react-hooks

Usage

在声明函数式组件的时候,通过用 withHooks() 函数封装就可以就使用 Hooks API 了。与 React 的 Hooks API 只差了需要 withHooks() 函数进行封装。

  1. import {withHooks, useState} from 'blx-react-hooks'
  2. // 如果想在调试的时候看到组件名,定义function的时候必须要定义成一个命名函数
  3. const Counter = withHooks(function Counter({title, initCount}) {
  4. const [count, setCount] = useState(initCount)
  5. return (
  6. <div>
  7. <h1>{title} </h1>
  8. <button onClick={() => setCount(count - 1)}> - </button>
  9. Count: {count}
  10. <button onClick={() => setCount(count + 1)}> + </button>
  11. </div>
  12. )
  13. })

完整示例 https://github.com/bramblex/react-hooks/blob/master/example.html

API

  • withHooks
  • useState / useReducer
  • useEffect / useLayoutEffect
  • useContext
  • useRef / useImperativeMethods
  • useMemo / useCallback

Document

API 的文档直接参见 React Hooks API 的文档:https://reactjs.org/docs/hooks-intro.html