Skip to content

Commit

Permalink
fix: cleanup ssr markup mismatch errors (#26)
Browse files Browse the repository at this point in the history
* fix: cleanup ssr markup mismatch errors

* feat: DRY with useIsMounted hook

* small nitpicks

* add notes about hydration workaround

Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
  • Loading branch information
davisshaver and holic committed Jun 27, 2022
1 parent fd30220 commit 5009d2b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/app/src/Inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useAccount, useNetwork } from "wagmi";
import { useInventoryQuery } from "./codegen/subgraph";
import { exampleNFTContract } from "./contracts";
import { PendingIcon } from "./PendingIcon";
import { useIsMounted } from "./useIsMounted";

gql`
query Inventory($owner: Bytes!) {
Expand All @@ -25,6 +26,14 @@ export const Inventory = () => {
},
});

// Temporarily workaround hydration issues where server-rendered markup
// doesn't match the client due to localStorage caching in wagmi
// See https://github.com/holic/web3-scaffold/pull/26
const isMounted = useIsMounted();
if (!isMounted) {
return null;
}

if (!address) {
return null;
}
Expand Down
14 changes: 12 additions & 2 deletions packages/app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import type { NextPage } from "next";
import { useExampleNFTContractRead } from "../contracts";
import { Inventory } from "../Inventory";
import { MintButton } from "../MintButton";
import { useIsMounted } from "../useIsMounted";

const HomePage: NextPage = () => {
const totalSupply = useExampleNFTContractRead("totalSupply", {
watch: true,
});
const maxSupply = useExampleNFTContractRead("MAX_SUPPLY");

const isMounted = useIsMounted();

return (
<div className="min-h-screen flex flex-col">
<div className="self-end p-2">
Expand All @@ -19,9 +22,16 @@ const HomePage: NextPage = () => {
<div className="flex-grow flex flex-col gap-4 items-center justify-center p-8 pb-[50vh]">
<h1 className="text-4xl">Example NFT</h1>

{/* Use isMounted to temporarily workaround hydration issues where
server-rendered markup doesn't match the client due to localStorage
caching in wagmi. See https://github.com/holic/web3-scaffold/pull/26 */}
<p>
{totalSupply.data?.toNumber().toLocaleString() ?? "??"}/
{maxSupply.data?.toNumber().toLocaleString() ?? "??"} minted
{(isMounted ? totalSupply.data?.toNumber().toLocaleString() : null) ??
"??"}
/
{(isMounted ? maxSupply.data?.toNumber().toLocaleString() : null) ??
"??"}{" "}
minted
</p>

<MintButton />
Expand Down
9 changes: 9 additions & 0 deletions packages/app/src/useIsMounted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect, useState } from "react";

export function useIsMounted() {
const [mounted, setMounted] = useState(false);

useEffect(() => setMounted(true), []);

return mounted;
}

0 comments on commit 5009d2b

Please sign in to comment.