Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strict mode rerendering issue #288

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 65 additions & 50 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,24 @@ import React, {
useMemo,
useState,
} from 'react';
import type { WalletSource } from '@vechain/dapp-kit';
import type { DAppKit, WalletSource } from '@vechain/dapp-kit';
import { DAppKitUI } from '@vechain/dapp-kit-ui';
import { subscribeKey } from 'valtio/vanilla/utils';
import type { DAppKitProviderOptions, DAppKitContext } from './types';
import { type Certificate } from '@vechain/sdk-core';
import type { DAppKitProviderOptions, DAppKitContext } from './types';

/**
* Context
*/
const Context = createContext<DAppKitContext | undefined>(undefined);

export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
export const DAppKitDataProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
connex,
}: {
children: React.ReactNode;
connex: DAppKit;
}): React.ReactElement => {
const connex = useMemo(
() =>
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
[
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
],
);

const [account, setAccount] = useState<string | null>(
connex.wallet.state.address,
);
Expand Down Expand Up @@ -146,6 +103,64 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
return <Context.Provider value={context}>{children}</Context.Provider>;
};

export const DAppKitProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}: DAppKitProviderOptions): React.ReactElement | null => {
const [connex, setConnex] = useState<DAppKit | null>(null);
useEffect(() => {
setConnex(
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
);
}, [
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
]);
if (!connex) {
return null;
}
return (
<DAppKitDataProvider connex={connex}>{children}</DAppKitDataProvider>
);
};

export const useConnex = (): DAppKitContext['connex'] => {
const context = useContext(Context);

Expand Down
Loading