Tram-One / tram-one

🚋 Legacy View Framework For Vanilla Javascript
http://tram-one.io/
MIT License
36 stars 8 forks source link

implement useMemo hook #126

Closed JRJurman closed 3 years ago

JRJurman commented 4 years ago

Summary

React has a useMemo hook used for persisting values that should not be re-evaluated if a set of dependencies has not been updated.

It's a similar interface to useEffect, but returns a value, and has no cleanup step. The useMemo hook also runs in the component evaluation, not after the component has rendered (so it will not be queued like effects are).

Potential Implementation

This feels like a simple enough hook to implement. If using useState, it's potentially possible to make this hook by storing both the dependencies and the last evaluated value, and checking if those dependencies have changed. If they haven't changed, you can return the value that is already saved, otherwise evaluate the function.

JRJurman commented 3 years ago

given the observable updates, this hook should no longer be required