Skip to content

Commit

Permalink
*
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Apr 20, 2024
1 parent 3e4e9e0 commit c0fa6dc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions coding_interviews/frontend/react/useMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useCallback } from 'react';

function useMap(initialValue) {
const [map, setMap] = useState(new Map(initialValue));

const set = useCallback((key, value) => {
setMap((prevMap) => {
const updatedMap = new Map(prevMap);
updatedMap.set(key, value);
return updatedMap;
});
}, []);

const del = useCallback((key) => {
setMap((prevMap) => {
const updatedMap = new Map(prevMap);
updatedMap.delete(key);
return updatedMap;
});
}, []);

const clear = useCallback(() => {
setMap((prevMap) => {
const updatedMap = new Map(prevMap);
updatedMap.clear();
return updatedMap;
});
}, []);

return {
map,
set,
delete: del,
clear,
};
}

0 comments on commit c0fa6dc

Please sign in to comment.