Skip to content

在 Vue3 中实现 React 原生 Hooks(useState、useEffect)进而深入理解 React Hooks 的本质原理

License

Notifications You must be signed in to change notification settings

amebyte/vue-hooks-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vue-hooks-api

Experimental React hooks implementation in Vue3

更多详细介绍请阅读我的掘金文章《在 Vue3 中实现 React 原生 Hooks(useState、useEffect)进而深入理解 React Hooks 的本质原理》

install

yarn add vue-hooks-api

React-style Hooks

import { useState, useReducer, useEffect, useLayoutEffect } from "vue-hooks-api";

const FunctionalComponent = (props, context) => {
  const [count1, setCount1] = useState(0);
  const [count2, setCount2] = useReducer((x) => x   1, 1);
  const [count3, setCount3] = useReducer((x) => x   1, 2);

  useEffect(() => {
    console.log("useEffect", count2);
  }, [count2]);

  useLayoutEffect(() => {
    console.log("useLayoutEffect", count2);
  }, [count2]);

  return (
    <>
      <button onClick={() => setCount1(2)} {...props}>
        count1:{count1}
      </button>
      <button onClick={() => setCount2()} {...props}>
        count2:{count2}
      </button>
      <button onClick={() => setCount3()} {...props}>
        count3:{count3}
      </button>
    </>
  );
};

export default FunctionalComponent;

About

在 Vue3 中实现 React 原生 Hooks(useState、useEffect)进而深入理解 React Hooks 的本质原理

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published