Skip to content

Commit

Permalink
fix: prevent rerender by router
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Nov 21, 2024
1 parent 8f90f27 commit bc53faf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
23 changes: 12 additions & 11 deletions src/app/explorer/components/InfiniteScrollGuilds.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";

import { Button } from "@/components/ui/Button";
import { Card } from "@/components/ui/Card";
import { Skeleton } from "@/components/ui/Skeleton";
import { env } from "@/lib/env";
import { useInfiniteQuery } from "@tanstack/react-query";
import { useIntersection } from "foxact/use-intersection";
Expand All @@ -23,7 +25,7 @@ export const InfiniteScrollGuilds = () => {
[search],
);

const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
useInfiniteQuery({
queryKey: ["guilds", search],
queryFn: fetchGuilds,
Expand Down Expand Up @@ -51,16 +53,15 @@ export const InfiniteScrollGuilds = () => {

return (
<section className="grid gap-2">
<h2 className="font-bold text-lg">Explore guilds</h2>
{guilds && guilds.length > 0 ? (
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{guilds.map((guild) => (
<GuildCard key={guild.id} guild={guild} />
))}
</div>
) : (
<p>Couldn&apos;t fetch guilds</p>
)}
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{isLoading
? Array.from({ length: 33 }, (_, i) => (
<Card key={i}>

Check notice on line 59 in src/app/explorer/components/InfiniteScrollGuilds.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/suspicious/noArrayIndexKey

Avoid using the index of an array as key property in an element.
<Skeleton className="size-full h-[114px]" />
</Card>
))
: guilds?.map((guild) => <GuildCard key={guild.id} guild={guild} />)}
</div>

<div
ref={(element: HTMLDivElement | null) => {
Expand Down
13 changes: 7 additions & 6 deletions src/app/explorer/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import { Input } from "@/components/ui/Input";
import { useDebouncedValue } from "foxact/use-debounced-value";
import { useSetAtom } from "jotai";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import { searchAtom } from "../atoms";

export const Search = () => {
const router = useRouter();
const searchParams = useSearchParams();
const pathname = usePathname();
const [value, setValue] = useState(
Expand All @@ -17,6 +16,7 @@ export const Search = () => {

const debouncedValue = useDebouncedValue(value, 200);
const setSearch = useSetAtom(searchAtom);

useEffect(() => {

Check warning on line 20 in src/app/explorer/components/Search.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/correctness/useExhaustiveDependencies

This hook does not specify all of its dependencies: setSearch
setSearch(debouncedValue);
}, [debouncedValue]);
Expand All @@ -25,10 +25,11 @@ export const Search = () => {
const newSearchParams = new URLSearchParams(
Object.entries({ search: value }).filter(([_, value]) => value),
);

router.replace(`${pathname}?${newSearchParams.toString()}`, {
scroll: false,
});
window.history.replaceState(
null,
"",
`${pathname}?${newSearchParams.toString()}`,
);
}, [value]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/app/explorer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function Explorer() {
<Search />
</section>

<InfiniteScrollGuilds search="haho" />
<InfiniteScrollGuilds />
</main>
);
}
Expand Down

0 comments on commit bc53faf

Please sign in to comment.