Skip to content

Commit

Permalink
Using cached chainId for controlling AlertModal
Browse files Browse the repository at this point in the history
Because chainId returns undefined while switching the provider
on useEthers activate/deactivate.
  • Loading branch information
wagyu298 committed Aug 9, 2023
1 parent 132607c commit 06959ef
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/nouns-webapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { ChainId, useEthers } from '@usedapp/core';
import { useAppDispatch, useAppSelector } from './hooks';
import { setActiveAccount } from './state/slices/account';
Expand All @@ -24,7 +24,8 @@ import dayjs from 'dayjs';
import DelegatePage from './pages/DelegatePage';

function App() {
const { account, chainId, library } = useEthers();
const { account, chainId, library, isLoading } = useEthers();
const [cachedChainId, setCachedChainId] = useState(chainId);
const dispatch = useAppDispatch();
dayjs.extend(relativeTime);

Expand All @@ -33,11 +34,17 @@ function App() {
dispatch(setActiveAccount(account));
}, [account, dispatch]);

useEffect(() => {
if (!isLoading) {
setCachedChainId(chainId);
}
}, [chainId, isLoading]);

const alertModal = useAppSelector(state => state.application.alertModal);

return (
<div className={`${classes.wrapper}`}>
{Number(CHAIN_ID) !== chainId && <NetworkAlert />}
{Number(CHAIN_ID) !== cachedChainId && <NetworkAlert />}
{alertModal.show && (
<AlertModal
title={alertModal.title}
Expand Down

0 comments on commit 06959ef

Please sign in to comment.