npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-reducers-hooks

v0.0.8

Published

Redux-like hooks for vue applications

Downloads

19

Readme

About

vue-reducers-hooks, inspired by Redux, is a package designed specifically for Vue applications. It provides two powerful composables that facilitate handling state in a Redux-like manner:

  • useReducer: a composable that allows you to manage state using a reducer function, providing a simple and predictable way to update state based on actions.

  • useCombineReducers: a composable that enables you to combine multiple reducers into a single state management function, making it easier to manage complex state across different parts of your application.

Getting Started

Install with npm: npm i vue-reducers-hooks

Reducer

Define your initial state and reducer function:

const todosState = {
  todos: [
    { id: 1, text: "Learn Vue.js", completed: false },
    { id: 2, text: "Build a to-do app", completed: false },
  ],
};

const todosReducer = (
  state = todosState,
  action:
    | { type: "ADD_TODO"; payload: { id: number; text: string } }
    | { type: "REMOVE_TODO"; payload: { id: number } }
): typeof todosState => {
  switch (action.type) {
    case "ADD_TODO": {
      const { id, text } = action.payload;
      return {
        ...state,
        todos: [...state.todos, { id, text, completed: false }],
      };
    }
    case "REMOVE_TODO": {
      const { id } = action.payload;
      return {
        ...state,
        todos: state.todos.filter((todo) => todo.id !== id),
      };
    }
    default:
      return state;
  }
};

Use the 'useReducer' composable in your component to manage state:

import { useReducer } from "vue-reducers-hooks";

const [state, dispatch] = useReducer(todosReducer, initialState);

where "state" represents the current state of your application, while "dispatch" is a function used to dispatch actions to update the state.

Combine Reducers

Use 'useCombineReducers' when your Vue application's state management becomes complex, requiring multiple reducers to manage different parts of your application's state. This composable offers a modular approach to state management, allowing you to combine and manage multiple reducers efficiently.

Define your reducers function:

const todosState = {
  todos: [
    { id: 1, text: "Learn Vue.js", completed: false },
    { id: 2, text: "Build a to-do app", completed: false },
  ],
};

const counterState = {
  count: 0,
};

const todosReducer = (
  state = todosState,
  action:
    | { type: "ADD_TODO"; payload: { id: number; text: string } }
    | { type: "REMOVE_TODO"; payload: { id: number } }
): typeof todosState => {
  switch (action.type) {
    case "ADD_TODO": {
      const { id, text } = action.payload;
      return {
        ...state,
        todos: [...state.todos, { id, text, completed: false }],
      };
    }
    case "REMOVE_TODO": {
      const { id } = action.payload;
      return {
        ...state,
        todos: state.todos.filter((todo) => todo.id !== id),
      };
    }
    default:
      return state;
  }
};

const counterReducer = (
  state = counterState,
  action: { type: "INCREMENT" } | { type: "DECREMENT" }
): typeof counterState => {
  switch (action.type) {
    case "INCREMENT":
      return { ...state, count: state.count + 1 };
    case "DECREMENT":
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
};

Then combine them:

import { useCombineReducers } from "vue-reducers-hooks";

const { dispatch, stateSelector } = useCombineReducers({
  todos: todosReducer,
  counter: counterReducer,
});

Dispatch actions

Update your reducers state by dispatching actions with the "dispatchCombineReducer" method. Simply specify the key of the reducer you wish to target for updating.

const todosDispatch = dispatch("todos");
const counterDispatch = dispatch("counter");

todosDispatch({ type: "ADD_TODO", payload: { id: 3, text: "new todo" } });

todosDispatch({ type: "REMOVE_TODO", payload: { id: 1 } });

counterDispatch({ type: "INCREMENT" });

Select state

Utilize the "stateSelector" method to access and extract specific portions of the global state. This method keeps the selected state reactive, ensuring your UI updates automatically when the state changes.

const updatedCounter = stateSelector((state) => state.counter?.count);

Contributing

Whether you want to report a bug, request a feature or submit a pull request, your contribution is greatly appreciated.

Don't forget to show your support by giving the project a star!

License

Distributed under the MIT License. See LICENSE for more information.