@kalu5/vue3-hooks

1.0.2 • Public • Published

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.

Advantage:

  1. Semantics is better.
  2. Logic is clearer.

I highly recommend using it.

Use

pnpm i @kalu5/vue3-hooks

useState

The correct way to open State.

eg:

<script setup lang="ts">
import { useState } from '@kalu5/vue3-hooks'
const [state, setState] = useState<number>(0)
</script>

<template>
  <h1>{{ state }}</h1>
  <button @click="setState((state) => state.value + 1)">
    Add
  </button>
  <button @click="setState((state) => state.value - 1)">
    Minus
  </button>
</template>

useReducer

eg:

<script setup lang="ts">
import { useReducer } from '@kalu5/vue3-hooks'
function reducer(state, action) {
  if (action.type === 'add')
    return state.value + 1
  return state.value - 1
}
const [state, dispatch] = useReducer<number>(reducer, 1)
const [newState, newDispatch] = useReducer<number>(reducer, 2, (init: number) => init * 3)
</script>

<template>
  <h1>{{ state }}</h1>
  <h2>{{ newState }}</h2>
  <button @click="dispatch({ type: 'add' })">
    Add
  </button>
  <button @click="dispatch({ type: 'minus' })">
    Minus
  </button>
</template>

Readme

Keywords

Package Sidebar

Install

npm i @kalu5/vue3-hooks

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

4.15 kB

Total Files

3

Last publish

Collaborators

  • kalu5