VueHooks Plus

repository·master·Indexed 24 days ago

https://github.com/inhiblabcore/vue-hooks-plus

A high-performance collection of Vue 3 hooks supporting SSR and TypeScript. It features a powerful useRequest middle tier for network requests, component hooks that encapsulate both logic and UI, and specialized utilities like useImmer for immutable state manipulation and useWorker. The library supports on-demand loading, CDN usage, and automatic imports via VueHooksPlusResolver for Vite and Webpack.

Tokens
75.6K
Snippets
158
Records
442
Agent score
83%

What's inside vue-hooks-plus

  1. Understand the vue-hooks-plus upgrade roadmap

    master

    The project is undergoing a comprehensive upgrade across four main phases to improve security, testing, Vue 3.5 compatibility, and source code security.

    Upgrade Phases:

    1. Toolchain & Security: Upgrading Vitest, Vite, and Prettier; clearing dependency vulnerabilities; establishing CI audit gates.
    2. Testing Improvements: Implementing coverage infrastructure (target: $\ge$ 80% line coverage for core packages) and filling testing gaps in auxiliary packages like use-url-state, use-immer, use-worker, use-request-plugins, and resolvers.
    3. Vue 3.5 Modernization: Updating peerDependencies to ^3.5 and refactoring internal implementations using new Vue 3.5 APIs (e.g., onWatcherCleanup, watch pause/resume).
    4. Source Code Security Audit: Reviewing attack surfaces in hooks like useExternal (script injection), useCookieState, and use-worker communication.
  2. What is a Component Hook?

    master
    A component hook is a factory function that returns a Vue component. Unlike standard hooks that only encapsulate logic, a component hook encapsulates both logic and UI. It internally uses a standard hook (such as useRequest) to manage state and lifecycle, but exposes that state through Vue slots, allowing you to reuse complex behaviors and layouts across your application.
  3. Reset and mutate data in useInfiniteScroll

    master

    Resetting Data

    To reset the list to the first page (e.g., when a filter changes), use the reload method.

    Pro-tip: Use the reloadDeps option to automatically trigger a reload whenever specific dependencies change. This acts as a syntax sugar for watching dependencies and calling reload() manually.

    Mutating Data

    To perform local updates without a full network reload (e.g., deleting a single item from the list), use the mutate method. You can pass a new version of the data object to mutate(newData) to replace the current state.

  4. Use refreshDeps in useRequest to trigger automatic re-requests

    master

    The refreshDeps option in useRequest allows you to specify reactive dependencies that, when changed, will automatically re-trigger the request. This functions similarly to Vue's watch mechanism but is integrated into the useRequest lifecycle.

    Important Note: refreshDeps only takes effect when the manual option is NOT set to true.

  5. Refresh requests using refreshDeps in useRequest

    master

    The useRequest hook provides a refreshDeps option to automatically re-trigger requests when reactive dependencies change. This serves as a replacement for the standard Vue watch mechanism within the context of a request.

    Important Constraint: refreshDeps only works in automatic mode. If manual is set to true, dependency refreshing will not trigger new requests.

  6. Perform optimistic updates with mutate()

    master

    The mutate function allows you to immediately update the data returned by useRequest without waiting for a network request to complete. This provides a snappier UI experience.

    Usage Patterns

    • mutate(newData): Replaces the current data with newData.
    • mutate((oldData) => newData): Updates data based on its previous state.

    Error Rollback

    To ensure data integrity if a background update fails, enable rollbackOnError. If the actual service call fails, the data will automatically revert to its previous state.

  7. Use the useBroadcastChannelPlugin to sync state across tabs

    master

    The useBroadcastChannelPlugin is a plugin for useRequest that enables broadcasting and synchronizing useRequest state (such as loading, data, or errors) between browser tabs or windows from the same origin. It uses the broadcast-channel API to facilitate this communication.

    import { useRequest } from 'vue-hooks-plus'
    import { useBroadcastChannelPlugin } from '@vue-hooks-plus/use-request-plugins'
    
    // Example usage within a component
    useRequest(
      service,
      {
        broadcastChannel: 'my-sync-channel',
      },
      [useBroadcastChannelPlugin],
    )