[Feat] Replace useMemo with useState for one time initializations

Open
#330 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Stale
Tech stack
react, typescript
Domain
frontend

Research direction

Start by inspecting the useMemo calls in examples/deckgl-overlay/src/deckgl-overlay.ts, src/components/map-control.tsx, and src/components/pin.tsx. Identify which initializations are intended to run only once and replace those usages with useState. Done means the relevant one-time initializations no longer rely on useMemo while preserving their current behavior.

Written by the indexing model from the issue text.

Description

idea
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:

https://github.com/visgl/react-google-maps/blob/8140b4ae6df22a74f7be253430554b07267bb944/examples/deckgl-overlay/src/deckgl-overlay.ts#L16
https://github.com/visgl/react-google-maps/blob/8140b4ae6df22a74f7be253430554b07267bb944/src/components/map-control.tsx#L50
https://github.com/visgl/react-google-maps/blob/8140b4ae6df22a74f7be253430554b07267bb944/src/components/pin.tsx#L22

Dominant language
TypeScript
Stars
2k
Forks
195
Avg merge
6h 26m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from visgl/react-google-maps

All issues in visgl/react-google-maps

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.