web
web copied to clipboard
Initializer function in `useMap()`, `useSet()`, etc.
New Features
It would be nice if useMap(), useSet(), etc. supported an initializer function for those initial values that can be expensive to compute, e.g.
const map = useMap(Object.entries(hugeObject).filter(...).map(([key]) => [key, ''])
Why should this feature be included?
~It can be done by keeping track of whether we're rendering for the first time with a useRef() and if so, setting the values...~
Actually, I think that would trigger a re-render immediately after the first render? A better solution is probably something like this:
let initialValue = useRef()
if (initialValue.current === undefined) {
initialValue.current = expensiveComputation()
}
const map = useMap(initialValue.current)
But that's not as nice as the first snippet, right?
Thank you!