1
import type { ConnectionConfig } from '@solana/web3.js';
import { Connection }enter preformatted text here from '@solana/web3.js';
import type { FC, ReactNode } from 'react';
import React, { useMemo } from 'react';
import { ConnectionContext } from './useConnection.js';

export interface ConnectionProviderProps {
    children: ReactNode;
    endpoint: string;
    config?: ConnectionConfig;
}

export const ConnectionProvider: FC<ConnectionProviderProps> = ({
    children,
    endpoint,
    config = { commitment: 'confirmed' },
}) => {
    const connection = useMemo(() => new Connection(endpoint, config), [endpoint, config]);

    return <ConnectionContext.Provider value={{ connection }}>{children}</ConnectionContext.Provider>;
};

The source code above is from @solana/wallet-adapter-react Mostly we will use this contextProvider on the top of the DOM tree like below.

<ConnectionProvider>
 <WalletProvider>
  <App/>
 </WalletProvider>
</ConnectionProvider>

And there comes the point that I'm wondering. What if the connection be disconnected? Then, the defined const variable "connection" will be useful regardlessly?

1 Answer 1

1

connection isn't a maintained connection (other than when using websocket)

You supply your RPC url and the Connection object will send HTTP requests when needed

If there is some issue with the RPC , a specific call will fail but the object is still valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.