Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/swr-devtools-demo/pages/infinite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DevToolsView } from "../components/DevToolsView";
import styles from "../styles/infinite.module.css";

export default function Home() {
const { data, setSize } = useSWRInfinite(
const { data, setSize, isValidating } = useSWRInfinite(
(index) => `/api/list?page=${index + 1}`
);

Expand All @@ -15,12 +15,12 @@ export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>SWR DevTools Demo (useSWRInfinite)</title>
<title>useSWRInfinite Demo</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>SWR DevTools Demo (useSWRInfinite)</h1>
<h1 className={styles.title}>useSWRInfinite Demo</h1>
<section className={styles.content}>
<ul className={styles.list}>
{pages.map((page) => (
Expand All @@ -33,15 +33,16 @@ export default function Home() {
</ul>
<div className={styles.buttonArea}>
<button
disabled={isValidating}
className={styles.button}
onClick={() => setSize((size) => size + 1)}
>
Load more
{isValidating ? "...loading" : "Load more"}
</button>
</div>
</section>
<nav className={styles.nav}>
<Link href="/">Top</Link>
<Link href="/">Go back</Link>
</nav>
</main>
<footer>
Expand Down
5 changes: 1 addition & 4 deletions examples/swr-devtools-demo/styles/infinite.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
}

.content {
display: flex;
flex-direction: column;
}

.list {
margin: 0 auto;
max-width: 80%;
display: flex;
flex-wrap: wrap;
gap: 1rem;
list-style: none;
}

.listItem {
font-size: 1.2rem;
padding-bottom: 10px;
}

.buttonArea {
Expand Down
7 changes: 5 additions & 2 deletions packages/swr-devtools-panel/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLayoutEffect, useMemo, useRef, useState } from "react";
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";

type EventListener = (...args: any[]) => void;
export type EventEmitter = {
Expand All @@ -15,13 +15,16 @@ type SWRRequest = {
error?: any;
};

export const useIsomorphicLayoutEffect =
typeof window === "undefined" ? useEffect : useLayoutEffect;

export type RequestsById = Record<string, SWRRequest[]>;

export function useRequests(events: EventEmitter | null) {
const [requestsById, setRequestsById] = useState<RequestsById>({});
const activeRequestsRef = useRef<Record<number | string, SWRRequest>>({});

useLayoutEffect(() => {
useIsomorphicLayoutEffect(() => {
// Which channel does this request belong to.
const idMap: Record<number, string> = {};
if (events === null) return;
Expand Down