Potential `useSubscribe()` improvements: dependency array and fixing React compiler warnings
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- javascript, react
調査の方向性
Start by reading the existing useSubscribe() implementation and how it calls useTracker(); the issue does not name the source file or tests. Compare its behavior with the two proposed dependency-aware versions, especially how subscription readiness responds when name or args change. Done means the maintainers decide which improvement to make and add coverage for the chosen behavior and React compiler warnings.
索引モデルが issue の本文から書いたものです。
説明
The problem
The regular import { useSuspense } from "meteor/react-meteor-data" works well in most cases. However, we had an overly complex (and likely buggy) component where the function returned by useSubscribe() always returned true even after the data clearly had loaded.
In normal cases, this is usually never a problem, but it did become a problem in our complex component which have excessive and inconsistent re-renders. Debugging the problem revealed that useSubscribe() seemingly doesn't track changes to name or args since it does not pass any dependency array to useTracker().
In our case, this led to the component getting stuck 5/6 times and only making it through rendering 1/6 times.
What fixed it locally in our project
After replacing useSubscribe() with useSubscribeWithDeps() as shown below, it always works, despite being used in a buggy component.
I tracked down the problem and patched it locally. Now I wonder if some of the following suggestions would be valuable to submit as PRs?
- Does it make sense to add the dependency array to the core
useSubscribe()? - Alternatively, would this be better suited as a new hook
useSubscribeWithDeps()? - Bonus: The current
useSubscribe()has several React compiler linting warnings because it mutates local variables potentially after rendering. Wrapping local state in refs solves this, while preserving the intended behavior.
Patched useSubscribeWithDeps()
/**
* Alternative to `useSubscribe()` that also tracks dependencies.
*
* This ensures that conditional subscriptions work as expected by
* rerunning the tracker when the `name` or `args` change.
*
* Also fixes React compiler warnings by using refs instead of local variables.
*/
export function useSubscribeWithDeps(name?: string, ...args: any[]): () => boolean {
const updateOnReady = useRef(false);
const subscription = useRef<Meteor.SubscriptionHandle>(null);
const isReady = useTracker(
() => {
if (!name) return true;
subscription.current = Meteor.subscribe(name, ...args);
return subscription.current.ready();
},
[name, EJSON.stringify(args as any)],
() => !updateOnReady.current,
);
return () => {
updateOnReady.current = true;
return !isReady;
};
}
Update
This version worked even more consistently in our app:
/**
* Alternative to `useSubscribe()` that also tracks dependencies.
*
* This ensures that conditional subscriptions work as expected by
* rerunning the tracker when the name or args change.
*/
export const useSubscribeWithDeps = (name?: string, ...args: any[]): (() => boolean) => {
const isReady = useTracker(() => {
if (!name) return true;
return Meteor.subscribe(name, ...args).ready();
}, [name, EJSON.stringify(args as any)]);
return () => !isReady;
};
- 主要言語
- JavaScript
- スター
- 577
- フォーク
- 165
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 1
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
meteor/react-packages のほかの issue
-
useTracker renders two times at mount, would be nice if it renders only once対応中かも @nicolv が 217 日前に担当しました。 オープンbug
難易度 3/5 1〜2日 初心者へのやさしさ 30/100
meteor/react-packages#439 · コメント 6 件 · リアクション 1 件 ·
-
Concurrency issue with useFind対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
meteor/react-packages#418 · コメント 8 件 · リアクション 1 件 ·
-
Suspense throws undefined error再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
meteor/react-packages#413 · コメント 4 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
meteor/react-packages#411 · コメント 3 件 ·
-
Dependent executionオープンdocumentation enhancement help wanted
難易度 5/5 1週間以上 初心者へのやさしさ 28/100
meteor/react-packages#407 · コメント 4 件 ·
meteor/react-packages の issue をすべて見る
似ている issue
-
Flow Needs: Triage :mag:
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
react/react-native#60447 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
erkamyaman/rust-anything#1153 ·
メンテナーはふだん 1 日以内に返信
-
Entity picker filter keeps the first kind's groups after switching Specific Term / Specific Postオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
davidofchatham/bws-gb-dynamic-tags-extensions#177 ·
メンテナーはふだん 1 日以内に返信
-
frontend good first issue kind:task level:starter size:s
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Arggon/ArggonStores#42 ·
メンテナーはふだん 1 日以内に返信
-
accessibility area: frontend area: i18n beginner-friendly bug difficulty: easy good first issue i18n keyboard priority: P3 quality user-experience
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
メンテナーはふだん 1 日以内に返信