lxsmnsyc / forgetti

Solve your hook spaghetti (with more spaghetti). Inspired by React Forget.
https://forgetti.vercel.app/
MIT License
352 stars 7 forks source link

Separate `$$ref` from `$$cache` #30

Closed SukkaW closed 1 year ago

SukkaW commented 1 year ago

The PR does 2 things:

This re-enables RSC support after 0.6.0.

SukkaW commented 1 year ago
  • Part of my decision on dropping useMemo is, as we know, how useMemo potentially resets state. Based on my observation, this doesn't seem to be the case for the production behavior, but more of the development behavior (related to react-refresh). So my concern still stands, as it affects the DX.

The new React Doc has explained when useMemo will throw away cache in detail: https://react.dev/reference/react/useMemo#caveats

And that's exactly why useMemo($$cache) and useRef($$ref) should be separated.

React expects useRef to hold the client-side only value (DOM Node, third-party libraries' instance like new VideoPlayer(), etc.), so refs will only be reset after components are unmounted.

React also expects useMemo for performance optimization, and React will throw away the cache when needed.

The PR makes the $$cache (and new $$ref) using the correct hook to meet React's expectations.

  • Minor thing is that this only solves RSC's concern. But that's just "minor".

Solving RSC compatibility is just a bonus as React's expectations have been met.

SukkaW commented 1 year ago

@lxsmnsyc Kindly request your attention with a friendly ping. I applied the fix to your requested changes 2 weeks ago. I would appreciate it if you could review it at your convenience and provide valuable feedback.

lxsmnsyc commented 1 year ago

@SukkaW Sorry I've been kinda busy recently. I'll give it a read tomorrow.

lxsmnsyc commented 1 year ago

Okay looks good. I'll release this some time this week.