"How to read an often-changing value from useCallback?" doesn't seem idiomatic
オープン
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 7.9k
- 平均マージ
- 1日 11時間
- マージ済み PR(30日)
- 11
説明
Custom hooks that take dependency arrays don't seem idiomatic (maybe I'm wrong). The biggest reason for that is that the React hooks lint doesn't check dependency arrays for anything except the primitive hooks.
In particular, I'd propose changing this:
function useEventCallback(fn, dependencies) {
const ref = useRef(() => {
throw new Error('Cannot call an event handler while rendering.');
});
useEffect(() => {
ref.current = fn;
}, [fn, ...dependencies]);
return useCallback(() => {
const fn = ref.current;
return fn();
}, [ref]);
}
to
function useEventCallback(fn) {
const ref = useRef(() => {
throw new Error('Cannot call an event handler while rendering.');
});
useEffect(() => {
ref.current = fn;
}, [fn]);
return useCallback(() => {
const fn = ref.current;
return fn();
}, []);
}
// which should be used as
const callback = useEventCallback(useCallback((event) => {
/* ... */
}, [/* dependencies */]));
This is slightly more abstract but also less likely to cause footguns. Alternatively, it might just be better to create a new function every render and store it in the ref (no use of dependencies at all).
function useEventCallback(fn) {
const ref = useRef(() => {
throw new Error('Cannot call an event handler while rendering.');
});
// Note this runs unconditionally
useEffect(() => {
ref.current = fn;
});
return useCallback(() => {
const fn = ref.current;
return fn();
}, []);
}
// which should be used as
const callback = useEventCallback((event) => {
/* ... */
});
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue では useEventCallback と useCallback が挙げられ、依存配列の例が 2 つ提示されていますが、リポジトリのファイルやテストは特定されていません。まず、これらのフックを扱っているドキュメントまたは例を見つけ、次にどのガイダンスを採用すべきかを判断し、合意した推奨事項で関連する資料を更新してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, react
- 領域
- documentation
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100