Implementing useMap hook
This time around we will implement a hook named useMap
, that will wrap around a native Map object and mimic its API. We're going to start with the tests this time.
The same logic applies. We want the actions that will change our state to stay the same no matter the changes of the state. In other words, only changes to the state should trigger a re-render of a component that gets state as a prop.
We'll start with the imports and test cases. For this, it's good to open the MDN documentation on Map
prototype and its methods.
We don't care about the reading part, we care about writing, because we need to create a new instance of the map in order to make it immutable to change. So we need delete
, clear
and set
. Also in react-hanger
I have added initialize
action in order to clear and override the whole map with fresh data in one go, which can be useful in some use cases.
So let's create the useMap.test.ts
file alongside our other tests.
// Walk through while writing the code
This page is a preview of The newline Guide to Creating a React Hooks Library