clawchain/wagmi/site/react/guides/migrate-from-v2-to-v3.md

4.3 KiB

title, description
title description
Migrate from v2 to v3 Guide for migrating from Wagmi v2 to v3.

Migrate from v2 to v3

Overview

Wagmi v3 gives you total control over connector dependencies. Since Wagmi's initial release, Wagmi included required connector dependencies as part of its package to eliminate the need to manage multiple third-party dependencies.

This worked worked well in the early years as a "batteries-included" approach, but didn't allow for more fine-grained control over your dependency tree. By giving you control over connector dependencies, you can decide to only install what you need, manage version bumps at your own pace, and have total control over what third-party code and licenses you bring into your project

To get started, install the latest version of Wagmi.

::: code-group

pnpm add wagmi@3
npm install wagmi@3
yarn add wagmi@3
bun add wagmi@3

:::

::: info Not ready to migrate yet? The Wagmi v2 docs are still available at 2.x.wagmi.sh/react. :::

Deprecations

Renamed Account Hooks

At the core of Wagmi are connections between apps and Ethereum providers (e.g. EIP-1193), useAccount, useAccountEffect, and useSwitchAccount are renamed to useConnection, useConnectionEffect, and useSwitchConnection to more accurately represent to how Wagmi works.

import {
  useAccount, // [!code --]
  useConnection, // [!code ++]
  useAccountEffect, // [!code --]
  useConnectionEffect, // [!code ++]
  useSwitchAccount, // [!code --]
  useSwitchConnection, // [!code ++]
} from 'wagmi'

Renamed mutateFn

Renamed custom mutate function names to mutate/mutateAsync to reduce destructure key renaming fatigue and align with TanStack Query terminology.

Before

Had to destructure hook result and often rename keys when using multiple of the same hook. Could decide not to destructure, but syntax becomes awkward for mutate functions (e.g. connect.connect or connect.connectAsync).

const { connect, isPending: connectIsPending } = useConnect()
const { writeContract: transfer, error: transferError, isPending: transferIsPending } = useWriteContract()
const { writeContract: approve, error: approveError } = useWriteContract()
After

Allows you to name the hook result whatever you want and not worry about also renaming properties.

const connect = useConnect() // connect.mutate, connect.isPending
const transfer = useWriteContract() // transfer.mutate, transfer.error, transfer.isPending
const approve = useWriteContract() // approve.mutate, approve.error

Removed useConnect().connectors & useReconnect().connectors

Moving forward, useConnect().connectors and useReconnect().connectors are no longer supported. Use useConnectors instead.

import { useConnect, useReconnect } from 'wagmi' // [!code --]
import { useConnectors } from 'wagmi' // [!code ++]

const { connectors } = useConnect() // [!code --]
const { connectors } = useReconnect() // [!code --]
const connectors = useConnectors() // [!code ++]

Removed useDisconnect().connectors & useSwitchConnection().connectors

Moving forward, useDisconnect().connectors and useSwitchConnection().connectors are no longer supported. Use useConnections instead.

import { useDisconnect, useSwitchConnection } from 'wagmi' // [!code --]
import { useConnections } from 'wagmi' // [!code ++]

const { connectors } = useDisconnect() // [!code --]
const { connectors } = useSwitchConnection() // [!code --]
const connections = useConnections() // [!code ++]
const connectors = connections.map((connection) => connection.connector) // [!code ++]

Removed useSwitchChain().chains

Moving forward, useSwitchChain().chains is no longer supported. Use useChains instead.

import { useSwitchChain } from 'wagmi' // [!code --]
import { useChains } from 'wagmi' // [!code ++]

const { chains } = useSwitchChain() // [!code --]
const chains = useChains() // [!code ++]