React Native
Introduction​
With Web3Modal React Native, we work with the Wagmi library which is a collection of React Hooks to make your dapp development flow easier. You can sign messages, interact with smart contracts, and much more.
Installation​
- React Native CLI
- Expo
- npm
- Yarn
- Bun
- pnpm
npm install @web3modal/wagmi-react-native wagmi viem
yarn add @web3modal/wagmi-react-native wagmi viem
bun add @web3modal/wagmi-react-native wagmi viem
pnpm add @web3modal/wagmi-react-native wagmi viem
Additionally add these extra packages to help with async storage, polyfills, and SVG's.
- npm
- Yarn
- Bun
- pnpm
npm install @react-native-async-storage/async-storage react-native-get-random-values react-native-svg react-native-modal @react-native-community/netinfo @walletconnect/react-native-compat
yarn add @react-native-async-storage/async-storage react-native-get-random-values react-native-svg react-native-modal @react-native-community/netinfo @walletconnect/react-native-compat
bun add @react-native-async-storage/async-storage react-native-get-random-values react-native-svg react-native-modal @react-native-community/netinfo @walletconnect/react-native-compat
pnpm add @react-native-async-storage/async-storage react-native-get-random-values react-native-svg react-native-modal @react-native-community/netinfo @walletconnect/react-native-compat
On iOS, use CocoaPods to add the native modules to your project:
npx pod-install
npx expo install @web3modal/wagmi-react-native wagmi viem
Additionally add these extra packages to help with async storage, polyfills, and SVG's.
npx expo install @react-native-async-storage/async-storage react-native-get-random-values react-native-svg react-native-modal @react-native-community/netinfo @walletconnect/react-native-compat
Additional setup for Expo SDK 48 only
If you are using Expo SDK 48, you also need to polyfill crypto
with expo-crypto library.
- Add
expo-crypto
npx expo install expo-crypto
- Create a file named
crypto-polyfill.js
// src/crypto-polyfill.js
// Apply only with Expo SDK 48
import { getRandomValues as expoCryptoGetRandomValues } from 'expo-crypto'
class Crypto {
getRandomValues = expoCryptoGetRandomValues
}
// eslint-disable-next-line no-undef
const webCrypto = typeof crypto !== 'undefined' ? crypto : new Crypto()
;(() => {
if (typeof crypto === 'undefined') {
Object.defineProperty(window, 'crypto', {
configurable: true,
enumerable: true,
get: () => webCrypto
})
}
})()
- Import
crypto-polyfill.js
in your App root file
// src/App.js
import './crypto-polyfill.js'
import '@walletconnect/react-native-compat';
...
import { createWeb3Modal, defaultWagmiConfig } from '@web3modal/wagmi-react-native'
Implementation​
Start by importing createWeb3Modal
, and wagmi packages, then create your configs as shown below.
Finally, pass your configuration to createWeb3Modal
.
Make sure you import @walletconnect/react-native-compat
before wagmi
to avoid any issues.
import '@walletconnect/react-native-compat';
import { WagmiConfig } from 'wagmi'
import { mainnet, polygon, arbitrum } from 'viem/chains'
import { createWeb3Modal, defaultWagmiConfig, Web3Modal } from '@web3modal/wagmi-react-native'
// 1. Get projectId at https://cloud.walletconnect.com
const projectId = 'YOUR_PROJECT_ID'
// 2. Create config
const metadata = {
name: 'Web3Modal RN',
description: 'Web3Modal RN Example',
url: 'https://web3modal.com',
icons: ['https://avatars.githubusercontent.com/u/37784886'],
redirect: {
native: 'YOUR_APP_SCHEME://',
universal: 'YOUR_APP_UNIVERSAL_LINK.com'
}
}
const chains = [mainnet, polygon, arbitrum]
const wagmiConfig = defaultWagmiConfig({ chains, projectId, metadata })
// 3. Create modal
createWeb3Modal({
projectId,
chains,
wagmiConfig
})
export default function App() {
return (
<WagmiConfig config={wagmiConfig}>
// Rest of your app...
<Web3Modal />
</WagmiConfig>
)
}
Trigger the modal​
To open Web3Modal you can use our default button component or build your own logic using Web3Modal hooks.
- Components
- Hooks
import { W3mButton } from '@web3modal/wagmi-react-native'
export default function ConnectView() {
return (
<>
...rest of your view
<W3mButton />
</>
)
}
Learn more about the Web3Modal components here
You can trigger the modal by calling the open
function from useWeb3Modal
hook.
import { Pressable, Text } from 'react-native'
import { useWeb3Modal } from '@web3modal/wagmi-react-native'
export default function ConnectView() {
const { open } = useWeb3Modal()
return (
<>
<Pressable onClick={() => open()}>
<Text>Open Connect Modal</Text>
</Pressable>
</>
)
}
Learn more about the Web3Modal hooks here
Enable Wallet Detection​
Make sure your have @walletconnect/react-native-compat@2.10.5
or higher.
To enable WalletConnectModal to detect wallets installed on the device, you need to make specific changes to the native code of your project.
- React Native CLI
- Expo
For iOS:​
- Open your
Info.plist
file. - Locate the
<key>LSApplicationQueriesSchemes</key>
section. - Add the desired wallet schemes as string entries within the
<array>
. These schemes represent the wallets you want to detect. - Refer to our Info.plist example file for a detailed illustration.
Example:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>metamask</string>
<string>trust</string>
<string>safe</string>
<string>rainbow</string>
<string>uniswap</string>
<!-- Add other wallet schemes names here -->
</array>
For Android:​
- Open your
AndroidManifest.xml
file. - Locate the
<queries>
section. - Add the desired wallet package names as
<package>
entries within the<queries>
. These package names correspond to the wallets you want to detect. - Refer to our AndroidManifest.xml example file for detailed guidance.
Example:
<queries>
<package android:name="io.metamask"/>
<package android:name="com.wallet.crypto.trustapp"/>
<package android:name="io.gnosis.safe"/>
<package android:name="me.rainbow"/>
<!-- Add other wallet package names here -->
</queries>
For iOS only:​
To enable WalletConnectModal to detect wallets installed on the device in your Expo project for iOS, follow these steps:
- Open your
app.json
(orapp.config.js
) file. - Locate the ios section within the configuration.
- Add the
infoPlist
object if it doesn't exist, and within it, include theLSApplicationQueriesSchemes
array. This array will contain the desired wallet schemes you want to detect. - Add the wallet schemes to the
LSApplicationQueriesSchemes
array.
Your configuration should look like this:
{
"expo": {
"ios": {
"infoPlist": {
"LSApplicationQueriesSchemes": [
"metamask",
"trust",
"safe",
"rainbow",
"uniswap"
// Add other wallet schemes names here
]
}
}
}
}
Was this helpful?