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

redux-memento

v1.1.1

Published

A redux-toolkit slice implementation for history state management

Downloads

8

Readme

Redux Memento Library

npm version

Overview

This library designed to simplify state management in Redux-toolkit applications by providing a mechanism for undo and redo functionality. The library integrates seamlessly with Redux-toolkit, allowing developers to create slices and middleware that support these time-traveling features.

Installation

Using NPM:

npm install redux-memento

or via Yarn:

yarn add redux-memento

Usage

  1. Define state and reducers types using IMementoSlice:
import { PayloadAction } from '@reduxjs/toolkit';
import { IMementoSlice } from 'redux-memento';

// Your state interface
export interface ICounter {
  value: number;
}

// Types for `createMementoSlice`:
export type CounterState = IMementoSlice<ICounter>;
export type CounterReducers = {
  increment: (state: CounterState) => void;
  decrement: (state: CounterState) => void;
  incrementByAmount: (state: CounterState, action: PayloadAction<number>) => void;
};

// Init your state as usual
const initialState: ICounter = {
  value: 0,
};
  1. Create a slice using createMementoSlice and utilize the types and interfaces you defined:
import { createMementoSlice } from 'redux-memento';

// ...

// Use the types you initialized before when creating the memento slice
// Initialize the slice as usual
export const counterSlice = createMementoSlice<ICounter, CounterReducers>({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state: CounterState) => {
      state.data.value += 1;
    },
    decrement: (state: CounterState) => {
      state.data.value -= 1;
    },
    incrementByAmount: (state: CounterState, action: PayloadAction<number>) => {
      state.data.value += action.payload;
    },
  },
});

export const { increment, decrement, incrementByAmount, redo, undo, setHistory } = counterSlice.actions;

export default counterSlice.reducer;
  1. Create a middleware using mementoMiddleware and apply it in the store. Specify the slice name, setHistory action, and actions to track state changes:
import { Store, configureStore } from '@reduxjs/toolkit';
import CounterReducer, { decrement, increment, setHistory } from '../features/counter/counterSlice';
import { mementoMiddleware } from 'redux-memento';

export const store: Store = configureStore({
  reducer: {
    counter: CounterReducer,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().prepend(mementoMiddleware('counter', setHistory, [increment, decrement]).middleware),
});
  1. Now, easily travel history with redo and undo:
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { increment, decrement, undo, redo } from '../features/counter/counterSlice';

export default function CounterMementoSlice() {
  const count = useSelector(({ counter }) => counter.data);
  const dispatch = useDispatch();

  const increaseCount = () => {
    dispatch(increment());
  };

  const decreaseCount = () => {
    dispatch(decrement());
  };

  const undoCounter = () => {
    dispatch(undo());
  };

  const redoCounter = () => {
    dispatch(redo());
  };

  return (
    <div>
      <h2>Counter: {count.value}</h2>
      <p>Using Memento Slice</p>
      <button onClick={increaseCount}>Increase</button>
      <button onClick={decreaseCount}>Decrease</button>
      <button onClick={undoCounter}>Undo</button>
      <button onClick={redoCounter}>Redo</button>
    </div>
  );
}

You can see the full example in the example folder.

How it works

You can read the read the story I went trough in this article.

Contributing

We welcome contributions! Before you start, please take a moment to review the installation guideline. After you finished your development, just make a PR.

Installation

  1. Clone the repository and make a fork:
git clone https://github.com/tomerbabila/redux-memento.git
  1. Create a new branch for your feature or bug fix:
git checkout -b feature/your-feature-name
  1. Install project dependencies:
npm ci
  1. If you wish to run the example app, run this command before:
npm link ../node_modules/react

This step is according to the first answer in this stackoverflow.

  1. Before pushing, make sure that everything is OK using these commands:
npm run prepare
npm run prepublishOnly