1 Star 0 Fork 0

daniel-dx/nice-hooks

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Nice Hooks

English

一些 Nice 的 Hooks,使得 React Hooks 更易于使用。

如果你发现该项目对你有用,请加个星吧。

安装

npm install nice-hooks

Hooks

让你可以安全地使用 react 的 state,它的值就是你想要的值,而不是陈旧的值。并且也拥有了 callback 的能力。

使用类似于 class 形式的 this.statethis.setState 的方式来使用 state。同样可以安全地使用 state,并且拥有 callback 能力

支持生命周期声明,以使代码组织更具可读性,而不是使用一堆 useEffect。

支持实例变量。即在每次重新渲染后,可获取该变量最新的值。

(推荐使用)将所有实例变量声明在一起,并以更接近实例变量的方式使用

使用

useStateCB

让你可以安全地使用 react 的 state,它的值就是你想要的值,而不是陈旧的值。并且也拥有了 callback 的能力。

# Example

import React from 'react';

import { useStateCB } from 'nice-hooks';

export const UseStateCBDemoComp = () => {
  const [getCount, setCount] = useStateCB(0);

  function doSomeActions() {
    console.log('Current count:', getCount());
  }

  return (
    <div>
      <p>{getCount()}</p>

      <button type="button" onClick={() => setCount(getCount() + 1, doSomeActions)}>
        Increase
      </button>
    </div>
  );
};

useSingleState

(推荐使用)使用类似于 class 形式的 this.statethis.setState 的方式来使用 state。同样可以安全地使用 state,并且拥有 callback 能力

# Example

import React from "react";

import { useSingleState } from "nice-hooks";

export const UseSingleStateDemoComp = () => {
  const [state, setState] = useSingleState({
    count: 0,
    time: +new Date()
  });

  function doSomeActions() {
    console.log("Current count:", state.count);
  }

  return (
    <div>
      <h2>useSingleState</h2>

      <p>{state.count} {state.time}</p>

      <button
        type="button"
        onClick={() =>
          setState(
            {
              count: state.count + 1
            },
            doSomeActions
          )
        }
      >
        Increase
      </button>
      <button type="button"
        onClick={() =>
          setState({
            time: +new Date()
          })
        }
      >
        Chnange Time
      </button>
    </div>
  );
};

useLifeCycle

支持生命周期声明,以使代码组织更具可读性,而不是使用一堆 useEffect。

# Example

import React from 'react';

import { useLifeCycle } from 'nice-hooks';

const App = () => {
  
  useLifeCycle({

    didMount() {
      // Do something after mounted
    },

    willUnmount() {
      // Do something when the component will be unmount
    },

    didUpdate() {
      // Do something after re-rendered.
    },

    didMountAndWillUnmount: [
      {
        didMount() {
          // Example: setTimeout
        },
        willUnmount() {
          // Example: clearTimeout
        }
      },
      {
        didMount() {
          // Example: on resize event
          // ...
        },
        willUnmount() {
          // Example: off resize event 
          // ...
        }
      }
    ]
  })

  return (
    <div></div>
  );
};

useInstanceVar

支持实例变量。即在每次重新渲染后,可获取该变量最新的值。

# Example

import React from "react";

import { useInstanceVar, useSingleState } from "nice-hooks";

export const UseInstanceVarDemoComp = () => {
  const [getIntervalVal, setIntervalVal] = useInstanceVar(null);

  const [state, setState] = useSingleState({ count: 0 });

  function start() {
    const interval = setInterval(
      () => setState({ count: state.count + 1 }),
      1000
    );
    setIntervalVal(interval);
  }

  function stop() {
    const interval = getIntervalVal();
    interval && clearInterval(interval);
  }

  return (
    <div>
      <p>{state.count}</p>
      <button onClick={start}>Start</button>
      <button onClick={stop}>Stop</button>
    </div>
  );
};

useSingleInstanceVar

(推荐使用)将所有实例变量声明在一起,并以更接近实例变量的方式使用

# Example

import React from "react";

import { useSingleInstanceVar, useSingleState } from "nice-hooks";

export const UseSingleInstanceVarDemoComp = () => {
  const instanceVal = useSingleInstanceVar({
    interval: null
  });

  const [state, setState] = useSingleState({ count: 0 });

  function start() {
    instanceVal.interval = setInterval(
      () => setState({ count: state.count + 1 }),
      1000
    );
  }

  function stop() {
    const interval = instanceVal.interval;
    interval && clearInterval(interval);
  }

  return (
    <div>
      <p>{state.count}</p>
      <button type="button" onClick={start}>Start</button>
      <button type="button" onClick={stop}>Stop</button>
    </div>
  );
};

贡献

  • git clone https://github.com/daniel-dx/nice-hooks.git
  • cd nice-hooks
  • npm install
  • npm run test
MIT License Copyright (c) 2019 Daniel Xiao Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

提供一些 nice 的 自定义 Hooks,使得 React Hooks 更易于使用。 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (2)

全部

近期动态

4年前推送了新的提交到 feature/demo 分支,65f17f7...612c562
4年前推送了新的提交到 feature/demo 分支,31e7444...65f17f7
4年前推送了新的 feature/demo 分支
5年多前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/danieldx/nice-hooks.git
git@gitee.com:danieldx/nice-hooks.git
danieldx
nice-hooks
nice-hooks
master

搜索帮助