UNPKG

react-hook-merge-state

Version:

在`useState`的基础上,对同一个状态的默认值、受控值进行对比监听

78 lines (58 loc) 2.02 kB
# react-hook-merge-state [![NPM version](https://img.shields.io/npm/v/react-hook-merge-state.svg?style=flat)](https://npmjs.org/package/react-hook-merge-state) [![NPM downloads](http://img.shields.io/npm/dm/react-hook-merge-state.svg?style=flat)](https://npmjs.org/package/react-hook-merge-state) ## 介绍 在`useState`的基础上,对同一个状态的默认值、受控值进行对比监听 ## 使用教程 ### 安装 ```bash pnpm add react-hook-merge-state ## 或者 npm i react-hook-merge-state ``` ### 快速上手 ```tsx import React from 'react'; import { useMergeState } from 'react-hook-merge-state'; interface State { hello: string; [key: string]: any; } export default () => { const [state, setState] = useMergeState<State>({ hello: '', }); useEffect(() => { console.log('状态值变化:', JSON.stringify(state, null, 2)); }, [state]); return ( <div> <pre>{JSON.stringify(state, null, 2)}</pre> <p> <button type="button" onClick={() => setState({ hello: 'world' })}> set hello </button> <button type="button" onClick={() => setState({ foo: 'bar' })} style={{ margin: '0 8px' }} > set foo </button> </p> </div> ); }; ``` ## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request ## 特技 1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)