Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit abd52d5

Browse files
committed
Update
1 parent a0574ad commit abd52d5

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

src/app/react/v5/migrate/contracts/page.mdx

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,95 @@ function App() {
7878
}
7979
```
8080

81-
As you can see from the example above, we introduced the hook [`useReadContract`](/references/typescript/v5/useReadContract) in v5. You should use it to perform any contract "read".
82-
It is the perfect replacement for the old chunky React hooks from v4.
81+
As you can see from the example above, we introduced the hook [`useReadContract`](/references/typescript/v5/useReadContract) in v5.
82+
You should use it to perform any contract "read" in your React app.
83+
It is the perfect replacement for the old chunky React hooks from v4.
84+
85+
The formula for reading a contract state is:
86+
```
87+
useReadContract + <the read contract method>
88+
```
89+
90+
#### If the extension you are looking for is not included in the SDK
91+
You can always use the function signature with `useReadContract` (It's also typesafe)
92+
```tsx
93+
useReadContract({
94+
contract,
95+
method: "function balanceOf(address _owner, uint256 tokenId) view returns (uint256)",
96+
...
97+
})
98+
```
99+
100+
#### Tips for getting a function's signature
101+
Go to the thirdweb Dashboard's explorer page and select the function that you want to interact with.
102+
You should see the "Use this function in your app" section with the code snippet for the signature of the function.
103+
104+
[An example](https://thirdweb.com/avalanche-fuji/0xd5e815241882676F772A624E3892b27Ff3a449c4/explorer?name=balanceOf)
105+
106+
107+
## Writing to a contract
108+
In v5, you can utilize the following hooks for writing to contracts: [`useSendTransaction`](/references/typescript/v5/useSendTransaction) and [`useSendAndConfirmTransaction`](/references/typescript/v5/useSendAndConfirmTransaction).
109+
The main difference between the 2 hooks is that `useSendTransaction` will mark the request as "complete" once the transaction is sent,
110+
while `useSendAndConfirmTransaction` will wait until the transaction is included in the blockchain.
111+
112+
Given the task of claiming an NFT from an NFT Drop collection, let's compare the code between the SDK v4 and v5
113+
114+
115+
#### SDK V4
116+
```tsx
117+
import { useContract, useClaimNFT } from "@thirdweb-dev/react";
118+
119+
function App() {
120+
const { contract } = useContract(contractAddress);
121+
const {
122+
mutateAsync: claimNft,
123+
isLoading,
124+
error,
125+
} = useClaimNFT(contract);
126+
127+
return (
128+
<Web3Button
129+
contractAddress={contractAddress}
130+
action={() =>
131+
claimNft({
132+
to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
133+
quantity: 1,
134+
})
135+
}
136+
>
137+
Claim NFT
138+
</Web3Button>
139+
);
140+
}
141+
```
142+
143+
#### SDK v5
144+
```tsx
145+
import { useSendTransaction } from "thirdweb/react";
146+
import { claimTo } from "thirdweb/extension/erc721";
147+
148+
function App() {
149+
const transaction = claimTo({ contract, quantity: 1n, to: "0x..." });
150+
const { mutateAsync: claimNft } = useSendTransaction();
151+
152+
return <button onClick={() => claimNft(transaction)}>
153+
Claim
154+
</button>
155+
}
156+
```
157+
158+
Another beautiful thing about the SDK v5 is that it comes with the [`TransactionButton`](/references/typescript/v5/TransactionButton)
159+
which allows you to make a contract call _without_ having to use the above React hooks. As you can see, the code is much cleaner this way!
160+
161+
```tsx
162+
import { TransactionButton } from "thirdweb/react";
163+
import { claimTo } from "thirdweb/extension/erc721";
164+
165+
function App() {
166+
return <TransactionButton transaction={
167+
() => claimTo({ contract, quantity: 1n, to: "0x..." })
168+
}>
169+
Claim
170+
</TransactionButton>
171+
}
172+
```

src/app/react/v5/migrate/installation/page.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ const client = createThirdwebClient({
9393
<ConnectButton client={client} />
9494
```
9595

96+
To learn more about the new `ConnectButton`, head over to the [Playground](https://thirdweb.com/dashboard/connect/playground).
97+
9698
Notice how you are passing the thirdweb client to the component itself and not to the `ThirdwebProvider` like in v4?
9799
By not putting every config inside the context wrapper, we were able to make the SDK v5 much more lightweight since you only load what you need!
98100

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# WIP

src/app/react/v5/sidebar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ export const sidebar: SideBar = {
305305
name: "Interacting with contracts",
306306
href: `${slug}/migrate/contracts`,
307307
},
308-
{
309-
name: "Using v5 with ethers",
310-
href: `${slug}/migrate/usage-v5-with-ethers`,
311-
},
308+
// {
309+
// name: "Using v5 with ethers",
310+
// href: `${slug}/migrate/use-v5-with-ethers`,
311+
// },
312312
{
313313
name: "Cheatsheet",
314314
href: `${slug}/migrate/cheatsheet`,

0 commit comments

Comments
 (0)