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 2 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
5 changes: 4 additions & 1 deletion 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,7 +26,9 @@ export const Inventory = () => {
},
});

if (!address) {
const isMounted = useIsMounted();

if (!address || !isMounted) {
return null;
}

Expand Down
13 changes: 9 additions & 4 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,10 +22,12 @@ 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>

<p>
{totalSupply.data?.toNumber().toLocaleString() ?? "??"}/
{maxSupply.data?.toNumber().toLocaleString() ?? "??"} minted
</p>
{isMounted && (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: I tend to prefer full ternaries like {condition ? (<>render</>) : null} as it avoids the edge case where your condition might not be a boolean and will unintentionally render the condition value itself.

Not sure if there is a linter for this kind of thing. I feel like I wrote one for this pattern at Stripe but I don't have the code anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@holic Cool, I hadn't thought about this much before but I agree. I wasn't able to find an existing rule for this, although there is one proposed here that we might be able to use: jsx-eslint/eslint-plugin-react#2888

I made #29 so we can track this in backlog

<p>
{totalSupply.data?.toNumber().toLocaleString() ?? "??"}/
{maxSupply.data?.toNumber().toLocaleString() ?? "??"} minted
</p>
)}

<MintButton />
<Inventory />
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;
}