import { useConnection} from '@solana/wallet-adapter-react';
const { connection } = useConnection();
here, how do we change the connection network? Is there any method like setConnection
or something similar?
You set the network usually in the ConnectionProvider with the endpoint prop.
// The network can be set to 'devnet', 'testnet', or 'mainnet-beta'.
const network = WalletAdapterNetwork.Mainnet;
// You can also provide a custom RPC endpoint.
const endpoint = useMemo(() => clusterApiUrl(network), [network]);
const wallets = useMemo(
() => [
new SolflareWalletAdapter(),
new PhantomWalletAdapter(),
new GlowWalletAdapter(),
new LedgerWalletAdapter(),
],
[]
);
return (
<ConnectionProvider endpoint={endpoint}>
<WalletProvider wallets={wallets} autoConnect>
<WalletModalProvider>{children}</WalletModalProvider>
</WalletProvider>
</ConnectionProvider>
);
};```
And instead of useConnection, you can also define it yourself.
```import { Connection } from '@solana/web3.js';```
And then set the connection
```const connection = new Connection('https://api.devnet.solana.com', "confirmed")
useConnection
hook if we can just initialize a new connection object ourselves everytime and the endpoint is not changable in the ConnectionProvider? I'm using some other packages and they use useConnection
hook, so I can't use my own connection object