1.8 KiB
1.8 KiB
title, tags, author, cover, firstSeen
| title | tags | author | cover | firstSeen |
|---|---|---|---|---|
| React useMap hook | hooks,state | chalarangelo | work-hard-computer | 2021-11-06T05:00:00-04:00 |
Creates a stateful Map object, and a set of functions to manipulate it.
- Use the
useState()hook and theMapconstructor to create a newMapfrom theinitialValue. - Use the
useMemo()hook to create a set of non-mutating actions that manipulate themapstate variable, using the state setter to create a newMapevery time. - Return the
mapstate variable and the createdactions.
const useMap = initialValue => {
const [map, setMap] = React.useState(new Map(initialValue));
const actions = React.useMemo(
() => ({
set: (key, value) =>
setMap(prevMap => {
const nextMap = new Map(prevMap);
nextMap.set(key, value);
return nextMap;
}),
remove: (key, value) =>
setMap(prevMap => {
const nextMap = new Map(prevMap);
nextMap.delete(key, value);
return nextMap;
}),
clear: () => setMap(new Map()),
}),
[setMap]
);
return [map, actions];
};
const MyApp = () => {
const [map, { set, remove, clear }] = useMap([['apples', 10]]);
return (
<div>
<button onClick={() => set(Date.now(), new Date().toJSON())}>Add</button>
<button onClick={() => clear()}>Reset</button>
<button onClick={() => remove('apples')} disabled={!map.has('apples')}>
Remove apples
</button>
<pre>
{JSON.stringify(
[...map.entries()].reduce(
(acc, [key, value]) => ({ ...acc, [key]: value }),
{}
),
null,
2
)}
</pre>
</div>
);
};
ReactDOM.createRoot(document.getElementById('root')).render(
<MyApp />
);