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: hydration error on dashboard #3444

Merged
merged 1 commit into from
Mar 19, 2024
Merged

fix: hydration error on dashboard #3444

merged 1 commit into from
Mar 19, 2024

Conversation

compojoom
Copy link
Contributor

When in dev mode and on a chain different than eth or sepolia every refresh display a “Hydration failed error”. It turns out that the governance module is being rendered on the server, where in the client it return null. This is caused by the use of useChainId hook which returns a default chain on the server, but on the client the actual chain the user is on. With the isLayoutReady check we make sure that this component actually only renders when ran on the client.

What it solves

Hydration error in dev mode

How this PR fixes it

Prevents rendering of the component on the server

How to test it

In dev mode switch the chain to something other than mainnet and sepolia. When on the dashboard refresh - you shouldn't see a hydration error.

Screenshots

Checklist

  • I've tested the branch on mobile 📱
  • I've documented how it affects the analytics (if at all) 📊
  • I've written a unit/e2e test for it (if applicable) 🧑‍💻

When in dev mode and on a chain different than eth or sepolia every refresh display a “Hydration failed error”. It turns out that the governance module is being rendered on the server, where in the client it return null.
This is caused by the use of useChainId hook which returns a default chain on the server, but on the client the actual chain the user is on.
With the isLayoutReady check we make sure that this component actually only renders when ran on the client.
@compojoom compojoom requested a review from schmanu March 15, 2024 07:58
Copy link

github-actions bot commented Mar 15, 2024

Copy link

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

Copy link

📦 Next.js Bundle Analysis for safe-wallet-web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

One Page Changed Size

The following page changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load
/home 53.38 KB (🟡 +18 B) 1.04 MB
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 20% or more, there will be a red status indicator applied, indicating that special attention should be given to this.

Copy link

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
79.38% (-0.02% 🔻)
11122/14011
🔴 Branches
58.4% (-0.02% 🔻)
2610/4469
🟡 Functions
66.1% (-0.04% 🔻)
1794/2714
🟢 Lines
80.64% (-0.02% 🔻)
10018/12423
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / index.tsx
82.35% (-8.82% 🔻)
66.67% (-16.67% 🔻)
50% (-12.5% 🔻)
83.87% (-9.68% 🔻)

Test suite run success

1411 tests passing in 195 suites.

Report generated by 🧪jest coverage report action from c30474d

Comment on lines +140 to +149
const [isLayoutReady, setIsLayoutReady] = useState(false)

useEffect(() => {
setIsLayoutReady(true)
}, [])

if (!isLayoutReady) {
return null
}

Copy link
Member

Choose a reason for hiding this comment

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

nit: Another way of doing this is using the dynamic import feature from nextjs. We might have to move the other component into a different file for this to work though.

const DynamicGovernanceSection = dynamic(() => import('./GovernanceSection'))

const GovernanceSectionWrapper = () => {
  const chainId = useChainId()
  if (!getSafeTokenAddress(chainId)) {
    return null
  }

  return <DynamicGovernanceSection />
}

@compojoom compojoom merged commit a5f7b31 into dev Mar 19, 2024
14 checks passed
@compojoom compojoom deleted the fix/hydration-error branch March 19, 2024 16:19
@github-actions github-actions bot locked and limited conversation to collaborators Mar 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants