Skip to content

Commit a7c640d

Browse files
committed
Display Network ID
1 parent f3f5e17 commit a7c640d

File tree

1 file changed

+7
-3
lines changed
  • templates/demo/web3js-react-dapp-demo/src

1 file changed

+7
-3
lines changed

templates/demo/web3js-react-dapp-demo/src/App.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function App() {
1919
}, [web3Context.providers.length]);
2020

2121
const [chainId, setChainId] = useState<bigint | undefined>(undefined);
22+
const [networkId, setNetworkId] = useState<bigint | undefined>(undefined);
2223
useEffect(() => {
2324
if (web3Context.currentProvider === undefined) {
2425
return;
@@ -30,13 +31,15 @@ function App() {
3031
setChainId(newId);
3132
}
3233

33-
function updateProviderChainId(newId: ProviderChainId) {
34+
function updateProviderIds(newId: ProviderChainId) {
3435
setChainId(BigInt(newId));
36+
web3Context.web3.eth.net.getId().then(setNetworkId);
3537
}
3638

3739
web3Context.web3.eth.getChainId().then(updateChainId);
38-
provider.on("chainChanged", updateProviderChainId);
39-
return () => provider.removeListener("chainChanged", updateProviderChainId);
40+
web3Context.web3.eth.net.getId().then(setNetworkId);
41+
provider.on("chainChanged", updateProviderIds);
42+
return () => provider.removeListener("chainChanged", updateProviderIds);
4043
}, [web3Context.currentProvider, web3Context.web3.eth]);
4144

4245
return (
@@ -116,6 +119,7 @@ function App() {
116119
) : null}
117120
<h2>Network Details</h2>
118121
<div>Chain ID: {`${chainId}`}</div>
122+
<div>Network ID: {`${networkId}`}</div>
119123
<AccountProvider>
120124
<Accounts></Accounts>
121125
</AccountProvider>

0 commit comments

Comments
 (0)