Skip to content

[Feat] Replace useMemo with useState for one time initializations #330

Open
@andrewdoro

Description

@andrewdoro

Target Use Case

We shouldn't have useMemo for one time initializations. While the current behaviour is the same as a useState, useMemo should only be used as an optimization. Future React releases might break this functionality.

https://tkdodo.eu/blog/use-state-for-one-time-initializations
React Forget

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

Proposal

Replace useMemo with useState for one time initializations. There might be more places where we need to change this:

const deck = useMemo(() => new GoogleMapsOverlay({interleaved: true}), []);

const controlContainer = useMemo(() => document.createElement('div'), []);

const glyphContainer = useMemo(() => document.createElement('div'), []);

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaNeat Idea

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions