Skip to content

Commit

Permalink
fix: FuelProvider leaking render events (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron authored Sep 30, 2024
1 parent 681785b commit f1a0a23
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-students-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuels/react": patch
---

Fixed FuelProvider triggering unnecessary render events
34 changes: 15 additions & 19 deletions examples/react-app/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,25 @@ const wagmiConfig = createConfig({
}),
],
});
const NETWORKS = [
{
chainId: CHAIN_IDS.fuel.testnet,
},
];

const FUEL_CONFIG = {
connectors: defaultConnectors({
devMode: true,
wcProjectId: WC_PROJECT_ID,
ethWagmiConfig: wagmiConfig,
chainId: CHAIN_IDS.fuel.testnet,
}),
};

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<FuelProvider
theme="dark"
uiConfig={{
suggestBridge: true, // default true
}}
networks={[
{
chainId: CHAIN_IDS.fuel.testnet,
},
]}
fuelConfig={{
connectors: defaultConnectors({
devMode: true,
wcProjectId: WC_PROJECT_ID,
ethWagmiConfig: wagmiConfig,
chainId: CHAIN_IDS.fuel.testnet,
}),
}}
>
<FuelProvider theme="dark" networks={NETWORKS} fuelConfig={FUEL_CONFIG}>
<Toast.Provider>
<App />
<Toast.Viewport
Expand Down
15 changes: 10 additions & 5 deletions packages/react/src/providers/FuelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { FuelConfig } from 'fuels';

import { Connect } from '../ui/Connect';

import { useMemo } from 'react';
import type { NetworkConfig, UIConfig } from '../types';
import { BridgeDialog } from '../ui/Connect/components/Bridge/BridgeDialog';
import { NetworkDialog } from '../ui/Connect/components/Network/NetworkDialog';
Expand Down Expand Up @@ -29,11 +30,15 @@ export function FuelProvider({
}: FuelProviderProps) {
const theme = _theme || 'light';
const networksConfig = useNetworkConfigs(networks);
const uiConfig = Object.assign(
{
suggestBridge: true,
},
_uiConfig ?? {},
const uiConfig = useMemo(
() =>
Object.assign(
{
suggestBridge: true,
},
_uiConfig ?? {},
),
[_uiConfig],
);

if (ui) {
Expand Down
89 changes: 56 additions & 33 deletions packages/react/src/providers/FuelUIProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ export function FuelUIProvider({
}
}, [connectors, connector]);

const handleBack = () => {
const handleBack = useCallback(() => {
setError(null);
setConnector(null);
setDialogRoute(Routes.LIST);
};
}, []);

const handleRetryConnect = useCallback(
async (connector: FuelConnector) => {
Expand Down Expand Up @@ -203,38 +203,61 @@ export function FuelUIProvider({
};
}, []);

const contextValue = useMemo(
() => ({
// General
theme: theme || 'light',
fuelConfig,
uiConfig,
error,
setError,
// Connection
isConnected: !!isConnected,
isConnecting,
// UI States
isLoading,
isError,
connectors,
// Actions
connect: handleConnect,
cancel: handleCancel,
// Dialog only
dialog: {
route: dialogRoute,
setRoute,
connector,
isOpen,
connect: handleSelectConnector,
retryConnect: handleRetryConnect,
back: handleBack,
_startConnection: handleStartConnection,
},
}),
[
theme,
fuelConfig,
uiConfig,
error,
isConnected,
isConnecting,
isLoading,
isError,
connectors,
connector,
dialogRoute,
isOpen,
handleCancel,
handleStartConnection,
setRoute,
handleSelectConnector,
handleConnect,
handleRetryConnect,
handleBack,
],
);

return (
<FuelConnectContext.Provider
value={{
// General
theme: theme || 'light',
fuelConfig,
uiConfig,
error,
setError,
// Connection
isConnected: !!isConnected,
isConnecting,
// UI States
isLoading,
isError,
connectors,
// Actions
connect: handleConnect,
cancel: handleCancel,
// Dialog only
dialog: {
route: dialogRoute,
setRoute,
connector,
isOpen,
connect: handleSelectConnector,
retryConnect: handleRetryConnect,
back: handleBack,
_startConnection: handleStartConnection,
},
}}
>
<FuelConnectContext.Provider value={contextValue}>
{children}
</FuelConnectContext.Provider>
);
Expand Down

0 comments on commit f1a0a23

Please sign in to comment.