/use-undo-state

Roll back your state!

Primary LanguageJavaScript


use-undo-state

Rollback your state

Description

A simple hook to rollback your state, and stand at your previous modified state.

Usage

import {useUndoState} from 'use-undo-state';

function useTodoList() {
 const [list, setList, rollbackList] = useUndoState([]);

 const addElementToList = useCallback(
  async (element) => {
   try {
     setList((prevList) => [...prevList, element]);
     await fetch(element); //simulating a put/post/etc
   } catch (error) {
     rollbackList();
     console.error(error);
   }
  }, [setList, rollbackList]);

 return { list, addElementToList };
}

You could check this example