Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleanup ssr markup mismatch errors #26

Merged
merged 4 commits into from
Jun 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}