# rio-react-hooks

> 模拟实现React-hooks

Latest version **1.0.1** (published 2020-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install rio-react-hooks
pnpm add rio-react-hooks
yarn add rio-react-hooks
bun add rio-react-hooks
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-03-09 |
| First published | 2020-03-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | player363 |
| Maintainers | gailrio |
| Keywords | react, hooks |

## Links

- npm: https://www.npmjs.com/package/rio-react-hooks
- npm.io page: https://npm.io/package/rio-react-hooks

## Dependencies (1)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.8.7

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-03-09
- 1.0.0 — 2020-03-09

## README

# rio-react-hooks

模拟实现React Hooks API。

已经实现的功能
- useState
- useEffect
- useLayoutEffect
- useContext
- useReducer
- useRef
- useMemo
- useCallback
- useImperativeHandle

### 使用示例

```
npm i rio-react-hooks
```

useState示例
```javascript
import React from 'react';
import {useHooks, useState} from 'rio-react-hooks'

const Counter = useHooks(() => {
  const [count, setCount] = useState(0);
  return <div>
    count: <span>{count}</span>
    <button onClick={() => setCount(count + 1)}>+1</button>
    <button onClick={() => setCount(preCount => preCount - 1)}>-1</button>
  </div>;
});
const App = useHooks(() => {
  const [isShow, onSwitch] = useState(true);
  return (
    <div className="App">
      {
        isShow &&
        <Counter init={0}/>
      }
      <button onClick={() => onSwitch(!isShow)}>switch</button>
    </div>
  );
});
```

useImperativeHandle/useRef示例
```javascript
import React, {forwardRef} from 'react';
import {useHooks, useRef, useImperativeHandle} from 'rio-react-hooks';

const FancyInput = forwardRef(useHooks(function (props, ref) {
  const inputRef = useRef();
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    }
  }));
  return <input ref={inputRef}/>;
}));

const App = useHooks(function Form() {
  const ref = useRef();
  return (
    <>
      <FancyInput ref={ref}/>
      <button onClick={() => ref.current.focus()}>focus</button>
    </>
  );
});
```

---

### 学习/参考资料

> [React Hooks 分享](https://github.com/OverWatcherX/ReactHooksPractice) - bramblex

> [Typescript 实现](https://github.com/bramblex/react-hooks) - bramblex

> [hooks 文档](https://react.docschina.org/docs/hooks-reference.html)

---
_Source: https://npm.io/package/rio-react-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
