Skip to content

Initializer function in useMap(), useSet(), etc. #1639

@benface

Description

@benface

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions