|
5 | 5 | ImageOffIcon, |
6 | 6 | } from "lucide-react"; |
7 | 7 | import Link from "next/link"; |
8 | | -import { useState } from "react"; |
| 8 | +import { useCallback, useRef, useState } from "react"; |
9 | 9 | import type { ThirdwebClient } from "thirdweb"; |
10 | 10 | import { useActiveWallet } from "thirdweb/react"; |
11 | 11 | import { |
@@ -68,7 +68,16 @@ export function LaunchTokenStatus(props: { |
68 | 68 | const { createTokenFunctions } = props; |
69 | 69 | const [steps, setSteps] = useState<MultiStepState<StepId>[]>([]); |
70 | 70 | const [isModalOpen, setIsModalOpen] = useState(false); |
71 | | - const [contractAddress, setContractAddress] = useState<string | null>(null); |
| 71 | + const [contractAddress, _setContractAddress] = useState<string | null>(null); |
| 72 | + |
| 73 | + // needed to add a ref to avoid `executeSteps` using the stale value of state `contractAddress` because of closure |
| 74 | + const contractAddressRef = useRef<string | null>(null); |
| 75 | + |
| 76 | + const setContractAddress = useCallback((address: string) => { |
| 77 | + _setContractAddress(address); |
| 78 | + contractAddressRef.current = address; |
| 79 | + }, []); |
| 80 | + |
72 | 81 | const activeWallet = useActiveWallet(); |
73 | 82 | const walletRequiresApproval = activeWallet?.id !== "inApp"; |
74 | 83 |
|
@@ -241,10 +250,10 @@ export function LaunchTokenStatus(props: { |
241 | 250 | : "ERC20Asset", |
242 | 251 | }); |
243 | 252 |
|
244 | | - if (contractAddress) { |
| 253 | + if (contractAddressRef.current) { |
245 | 254 | props.onLaunchSuccess({ |
246 | 255 | chainId: Number(formValues.chain), |
247 | | - contractAddress, |
| 256 | + contractAddress: contractAddressRef.current, |
248 | 257 | }); |
249 | 258 | } |
250 | 259 | } |
|
0 commit comments