How to Implement WalletConnect in your React Native App
To connect WalletConnect with a React Native app, you will need to do the following:
- Install the
@walletconnect/react-native
package from NPM. You can do this by running the following command:
npm install @walletconnect/react-native
2. Import the WalletConnect, QRCodeModal, and Session Provider components from the @walletconnect/react-native
package in your React Native app.
import { WalletConnect, QRCodeModal, SessionProvider } from '@walletconnect/react-native'
3. Add the WalletConnect, QRCodeModal, and Session Provider components to your React Native app. You can do this by wrapping your app’s root component with the SessionProvider
component and adding the WalletConnect
and QR Code Modal
components where you want them to be displayed in your app.
<SessionProvider>
<WalletConnect />
<QR Code Modal />
<YourAppRootComponent />
</SessionProvider>
4. Initialize the WalletConnect instance by passing in the required bridge
and dappName
parameters. The bridge
parameter is the URL of the WalletConnect bridge server that you want to use, and the dappName
parameter is the name of your React Native app.
const walletConnector = new WalletConnect({
bridge: 'https://bridge.walletconnect.org',
dappName: 'My React Native App'
})
5. Open the WalletConnect session by calling the createSession
method on the walletConnector
instance. This will open the WalletConnect QR Code modal, which your users can scan with their WalletConnect-enabled wallet to establish a connection.
walletConnector.createSession()
6. Listen for session updates by subscribing to the session
event on the walletConnector
instance. This event will be triggered whenever the WalletConnect session is updated, and it will provide you with the latest session data, including the connected wallet's address and chain ID.
walletConnector.on('session', (error, payload) => {
if (error) {
// handle error
} else {
// use the payload data to update your app's state
}
})
7. Use the connected wallet’s address and chain ID to sign transactions and interact with the blockchain.
8. Close the WalletConnect session when your users are done using it by calling the killSession
method on the walletConnector
instance.
walletConnector.killSession()
Here is a full example of how to connect WalletConnect with a React Native app:
import React from 'react'
import { View } from 'react-native'
import { WalletConnect, QRCodeModal, SessionProvider } from '@walletconnect/react-native'
const App = () => {
const walletConnector = new WalletConnect({
bridge: 'https://bridge.walletconnect.org',
dappName: 'My React Native App'
})
walletConnector.createSession()
walletConnector.on('session', (error, payload) => {
if (error) {
// handle error
} else {
// use the payload data to update your app's state
}
})
return (
<SessionProvider>
<WalletConnect />
<QRCodeModal />
<View>
{/* Your app's components go here */}
</View>
</SessionProvider>
)
}
export default App
I hope this helps! Let me know if you have any other questions.