Reactのcache()
wrapされた関数の結果を記憶する関数
同じ引数で呼び出された同じ関数は、関数を再実行する代わりにcache値を返す
code:ts
// A cached function returns the same response for a given set of inputs —
// id, in this example. The cache proposal will also have a mechanism to
// invalidate the cache, and to scope it to a particular part of the UI.
const fetchNote = cache(async (id) => {
const response = await fetch(/api/notes/${id});
return await response.json();
});
function Note({id}) {
// The fetchNote call returns the same promise every time until a new id
// is passed, or until the cache is refreshed.
const note = use(fetchNote(id));
return (
<div>
<h1>{note.title}</h1>
<section>{note.body}</section>
</div>
);
}