Potential `useSubscribe()` improvements: dependency array and fixing React compiler warnings

オープン
#506 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
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?

  1. Does it make sense to add the dependency array to the core useSubscribe()?
  2. Alternatively, would this be better suited as a new hook useSubscribeWithDeps()?
  3. 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

meteor/react-packages のほかの issue

meteor/react-packages の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。