javascript - App wide RTK query loading & fetching indicator - Stack Overflow

In my React app, I am using RTK query to fetch data. I have 10+ API endpoints and a route for each one

In my React app, I am using RTK query to fetch data. I have 10+ API endpoints and a route for each one of the endpoints.

I want to show a horizontal line at the top of the page to indicate that the data fethcing is going on.

At the moment, I have done this.

const ArticlePage = () => {
  const { id} = useParams();
  const { data, isFetching, isLoading } = useFetchArticleByIdQuery(id);

  return (
    <Layout>
      {(isFetching || isLoading) && <LinearProgressIndicator />}
      <PageHead />
      {data && <ArticleContent article={data} />}
    </Layout>
  );
};

I have also tried this.

const ArticlePage = () => {
  const { id} = useParams();
  const { data, isFetching, isLoading } = useFetchArticleByIdQuery(id);

  return (
    <Layout>
      <LinearProgressIndicator shouldDisplay={(isFetching || isLoading)} />
      <PageHead />
      {data && <ArticleContent article={data} />}
    </Layout>
  );
};

It is cumbersome ot use the same thing on all the 10+ routes. So is there any way to show <LinearProgressIndicator /> once for all the 10+ endpoints?

In my React app, I am using RTK query to fetch data. I have 10+ API endpoints and a route for each one of the endpoints.

I want to show a horizontal line at the top of the page to indicate that the data fethcing is going on.

At the moment, I have done this.

const ArticlePage = () => {
  const { id} = useParams();
  const { data, isFetching, isLoading } = useFetchArticleByIdQuery(id);

  return (
    <Layout>
      {(isFetching || isLoading) && <LinearProgressIndicator />}
      <PageHead />
      {data && <ArticleContent article={data} />}
    </Layout>
  );
};

I have also tried this.

const ArticlePage = () => {
  const { id} = useParams();
  const { data, isFetching, isLoading } = useFetchArticleByIdQuery(id);

  return (
    <Layout>
      <LinearProgressIndicator shouldDisplay={(isFetching || isLoading)} />
      <PageHead />
      {data && <ArticleContent article={data} />}
    </Layout>
  );
};

It is cumbersome ot use the same thing on all the 10+ routes. So is there any way to show <LinearProgressIndicator /> once for all the 10+ endpoints?

Share Improve this question edited Mar 18, 2022 at 7:55 mahan asked Mar 18, 2022 at 7:50 mahanmahan 15.1k8 gold badges67 silver badges98 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can write a selector for that:

  const isLoading = useAppSelector((state) => {
    return Object.values(state.api.queries).some((query) => {
      return query && query.status === QueryStatus.pending;
    });
  });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744288774a4566931.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信