Skip to content

Commit 5ff7e55

Browse files
fix: move useCallback hooks before early returns in React hooks
This fixes the 'Rendered more hooks than during the previous render' error that occurs when using useAppKitConnections and useAppKitConnection hooks. The issue was that useCallback hooks were being called after conditional early returns, which violates React's rules of hooks - hooks must be called unconditionally in the same order on every render. Changes: - useAppKitConnections: moved formatConnection useCallback before the isMultiWalletEnabled early return - useAppKitConnection: moved switchConnection and deleteConnection useCallback hooks before the isMultiWalletEnabled early return Co-Authored-By: enes@reown.com <enes@reown.com>
1 parent 23bb47b commit 5ff7e55

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

packages/controllers/exports/react.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,6 @@ export function useAppKitConnections(namespace?: ChainNamespace) {
167167
throw new Error('No namespace found')
168168
}
169169

170-
const { connections, recentConnections } =
171-
ConnectionControllerUtil.getConnectionsData(chainNamespace)
172-
173-
if (!isMultiWalletEnabled) {
174-
AlertController.open(
175-
ConstantsUtil.REMOTE_FEATURES_ALERTS.MULTI_WALLET_NOT_ENABLED.CONNECTIONS_HOOK,
176-
'info'
177-
)
178-
179-
return {
180-
connections: [],
181-
recentConnections: []
182-
}
183-
}
184-
185170
const formatConnection = useCallback((connection: Connection) => {
186171
const connector = ConnectorController.getConnectorById(connection.connectorId)
187172

@@ -197,6 +182,21 @@ export function useAppKitConnections(namespace?: ChainNamespace) {
197182
}
198183
}, [])
199184

185+
const { connections, recentConnections } =
186+
ConnectionControllerUtil.getConnectionsData(chainNamespace)
187+
188+
if (!isMultiWalletEnabled) {
189+
AlertController.open(
190+
ConstantsUtil.REMOTE_FEATURES_ALERTS.MULTI_WALLET_NOT_ENABLED.CONNECTIONS_HOOK,
191+
'info'
192+
)
193+
194+
return {
195+
connections: [],
196+
recentConnections: []
197+
}
198+
}
199+
200200
return {
201201
connections: connections.map(formatConnection),
202202
recentConnections: recentConnections.map(formatConnection)
@@ -217,24 +217,6 @@ export function useAppKitConnection({ namespace, onSuccess, onError }: UseAppKit
217217

218218
const isMultiWalletEnabled = Boolean(remoteFeatures?.multiWallet)
219219

220-
if (!isMultiWalletEnabled) {
221-
AlertController.open(
222-
ConstantsUtil.REMOTE_FEATURES_ALERTS.MULTI_WALLET_NOT_ENABLED.CONNECTION_HOOK,
223-
'info'
224-
)
225-
226-
return {
227-
connection: undefined,
228-
isPending: false,
229-
switchConnection: () => Promise.resolve(undefined),
230-
deleteConnection: () => ({})
231-
}
232-
}
233-
234-
const connectorId = activeConnectorIds[chainNamespace]
235-
const connList = connections.get(chainNamespace)
236-
const connection = connList?.find(c => c.connectorId.toLowerCase() === connectorId?.toLowerCase())
237-
238220
const switchConnection = useCallback(
239221
async ({ connection: _connection, address }: SwitchConnectionParams) => {
240222
try {
@@ -284,6 +266,24 @@ export function useAppKitConnection({ namespace, onSuccess, onError }: UseAppKit
284266
[chainNamespace]
285267
)
286268

269+
if (!isMultiWalletEnabled) {
270+
AlertController.open(
271+
ConstantsUtil.REMOTE_FEATURES_ALERTS.MULTI_WALLET_NOT_ENABLED.CONNECTION_HOOK,
272+
'info'
273+
)
274+
275+
return {
276+
connection: undefined,
277+
isPending: false,
278+
switchConnection: () => Promise.resolve(undefined),
279+
deleteConnection: () => ({})
280+
}
281+
}
282+
283+
const connectorId = activeConnectorIds[chainNamespace]
284+
const connList = connections.get(chainNamespace)
285+
const connection = connList?.find(c => c.connectorId.toLowerCase() === connectorId?.toLowerCase())
286+
287287
return {
288288
connection,
289289
isPending: isSwitchingConnection,

0 commit comments

Comments
 (0)