Git Product home page Git Product logo

vue3-hooks's Introduction

Vue3 hooks

npm package npm download github license github issues open github issues closed github language top github stars

NPM

Using vue3.x composition api in react-hooks style.

The react-like vue3 hooks are implemented just with composition api, requiring no any other dependencies but vue3.

If you are used to react hooks, and new to vue 3, you will try this. In this case, however, I suggest learning original composition api. It's easy :D.

What `vue3-hooks` really make sense is that so many third-party hooks library based on react can now easily migrate to vue technolegy stack. If you are an author of third-party react hooks, you may consider to use `vue3-hooks` to migrate your library to vue family. Very few code modification is required.

install

npm install --save vue3-hooks

features

  • useState (use ref and reactive from vue)
  • useEffect (use watch from vue)
  • WIP: more react-like hooks

useCallback is no need because setup will only been run once.

usage

import { computed } from 'vue';
import { useState, useEffect } from 'vue3-hooks';
import ajaxFetch from 'ajax-fetch-esm';
// ...

<p>{{ id }}</p>
<p>Counter: {{ count }}</p>
<p>{{ data.toString() }}</p>

// ...
setup(props) {
  // ...

  // all vue apps prefer using counter for example instead of hello world.
  const [count, setCount] = useState(0);
  const [data, setData] = useState({});

  // yes, vue3 composition api allow us passing an async function.
  // normal functions are also ok.
  // `() => props.id` means watching a computed vue3 variable.
  useEffect(async ([nextCount, nextId], [prevCount, prevId]) => {
    const timer = setTimeout(() => {
      setData(await ajaxFetch.get({ current: nextCount, id: nextId }));
    }, 500);
    // clean up
    return () => {
      clearTimeout(timer);
    };
  }, [count, () => props.id]);

  // ...
  return {
    // ...
    count,
    data,
    id: computed(() => props.id),
    // ...
  };
}
// ...

Recently updated

See CHANGELOG here.

LICENSE

MIT

vue3-hooks's People

Contributors

shallinta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xq0118

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.