4.1.10 • Published 5 months ago

@thirdweb-dev/react v4.1.10

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Installation

The easiest way to get started with the React SDK is to use the CLI:

npx thirdweb create --app

Alternatively, you can install the SDK into your existing project using npm or yarn:

npm install @thirdweb-dev/react @thirdweb-dev/sdk ethers
yarn add @thirdweb-dev/react @thirdweb-dev/sdk ethers

Getting Started

Our SDK uses a Provider Pattern; meaning any component within the ThirdwebProvider will have access to the SDK. If you use the CLI to initialize your project, this is already set up for you.

Let's take a look at a typical setup:

Configure the ThirdwebProvider

Specify the network your smart contracts are deployed to in the desiredChainId prop and wrap your application like so:

import { ChainId, ThirdwebProvider } from "@thirdweb-dev/react";

const App = () => {
  return (
    <ThirdwebProvider desiredChainId={ChainId.Mainnet}>
      <YourApp />
    </ThirdwebProvider>
  );
};

Below are examples of where to set this up in your application:

Connect to a User's Wallet

Now the provider is set up, we can use all of the hooks and UI components available in the SDK, such as the ConnectWallet component.

Once the user has connected their wallet, all the calls we make to interact with contracts using the SDK will be on behalf of the user.

import { ConnectWallet, useAddress } from "@thirdweb-dev/react";

export const YourApp = () => {
  const address = useAddress();
  return (
    <div>
      <ConnectWallet />
    </div>
  );
};

The ConnectWallet component handles everything for us, including switching networks, accounts, displaying balances and more.

We can then get the connected address using the useAddress hook anywhere in the app.

Interact With Contracts

Connect to your smart contract using the useContract hook like so:

import { useContract } from "@thirdweb-dev/react";

export default function Home() {
  const { contract } = useContract("<CONTRACT_ADDRESS>");

  // Now you can use the contract in the rest of the component!
}

You can then use useContractRead and useContractWrite to read data and write transactions to the contract.

You pass the contract object returned from useContract to these hooks as the first parameter and the name of the function (or view/mapping, etc.) on your smart contract as the second parameter. If your function requires parameters, you can pass them as additional arguments.

For example, we can read the name of our contract like so:

import {
  useContract,
  useContractRead,
  useContractWrite,
} from "@thirdweb-dev/react";

export default function Home() {
  const { contract } = useContract("<CONTRACT_ADDRESS>");
  const { data: name, isLoading: loadingName } = useContractRead(
    contract,
    "name", // The name of the view/mapping/variable on your contract
  );
  const { mutate: setName, isLoading: settingName } = useContractWrite(
    contract,
    "setName", // The name of the function on your contract
  );
}

Using Extensions

Each extension you implement in your smart contract unlocks new functionality in the SDK.

These hooks make it easy to interact with your smart contracts by implementing the complex logic for you under the hood.

For example, if your smart contract implements ERC721Supply, you unlock the ability to view all NFTs on that contract using the SDK; which fetches all of your NFT metadata and the current owner of each NFT in parallel. In the React SDK, that is available using useNFTs:

import { useContract, useNFTs } from "@thirdweb-dev/react";

export default function Home() {
  const { contract } = useContract("<CONTRACT_ADDRESS>");
  const { data: nfts, isLoading: isReadingNfts } = useNFTs(contract);
}

If we want to mint an NFT and our contract implements ERC721Mintable, we can use the useMintNFT hook to mint an NFT from the connected wallet; handling all of the logic of uploading and pinning the metadata to IPFS for us behind the scenes.

import { useContract, useNFTs, useMintNFT } from "@thirdweb-dev/react";

export default function Home() {
  const { contract } = useContract("<CONTRACT_ADDRESS>");
  const { data: nfts, isLoading: isReadingNfts } = useNFTs(contract);
  const { mutate: mintNFT, isLoading: isMintingNFT } = useMintNFT(contract);
}

UI Components

The SDK provides many UI components to help you build your application.

For example, we can render each of the NFTs using the NFT Media Renderer component, making use of the loading state from useNFTs:

import { useContract, useNFTs, ThirdwebNftMedia } from "@thirdweb-dev/react";

export default function Home() {
  const { contract } = useContract("<CONTRACT_ADDRESS>");
  const { data: nfts, isLoading: isReadingNfts } = useNFTs(contract);

  return (
    <div>
      <h2>My NFTs</h2>
      {isReadingNfts ? (
        <p>Loading...</p>
      ) : (
        <div>
          {nfts.map((nft) => (
            <ThirdwebNftMedia
              key={nft.metadata.id}
              metadata={nft.metadata}
              height={200}
            />
          ))}
        </div>
      )}
    </div>
  );
}

The Web3Button component ensures the user has connected their wallet and is currently configured to the same network as your smart contract before calling the function. It also has access to the contract directly, allowing you to perform any action on your smart contract when the button is clicked.

For example, we can mint an NFT like so:

import {
  useContract,
  useNFTs,
  ThirdwebNftMedia,
  Web3Button,
} from "@thirdweb-dev/react";

const contractAddress = "<CONTRACT_ADDRESS>";
export default function Home() {
  const { contract } = useContract(contractAddress);
  const { data: nfts, isLoading: isReadingNfts } = useNFTs(contract);

  return (
    <div>
      {/* ... Existing Display Logic here ... */}

      <Web3Button
        contractAddress={contractAddress}
        action={(contract) =>
          contract.erc721.mint({
            name: "Hello world!",
            image:
              // You can use a file or URL here!
              "ipfs://QmZbovNXznTHpYn2oqgCFQYP4ZCpKDquenv5rFCX8irseo/0.png",
          })
        }
      >
        Mint NFT
      </Web3Button>
    </div>
  );
}

Advanced Configuration

The ThirdwebProvider offers a number of configuration options to control the behavior of the React and Typescript SDK.

These are all the configuration options of the <ThirdwebProvider />. We provide defaults for all of these, but you customize them to suit your needs.

import { ChainId, IpfsStorage, ThirdwebProvider } from "@thirdweb-dev/react";

const KitchenSinkExample = () => {
  return (
    <ThirdwebProvider
      desiredChainId={ChainId.Mainnet}
      chainRpc={{ [ChainId.Mainnet]: "https://mainnet.infura.io/v3" }}
      dAppMeta={{
        name: "Example App",
        description: "This is an example app",
        isDarkMode: false,
        logoUrl: "https://example.com/logo.png",
        url: "https://example.com",
      }}
      storageInterface={new IpfsStorage("https://your.ipfs.host.com")}
      supportedChains={[ChainId.Mainnet]}
      walletConnectors={[
        "walletConnect",
        { name: "injected", options: { shimDisconnect: false } },
        {
          name: "walletLink",
          options: {
            appName: "Example App",
          },
        },
        {
          name: "magic",
          options: {
            apiKey: "your-magic-api-key",
            rpcUrls: {
              [ChainId.Mainnet]: "https://mainnet.infura.io/v3",
            },
          },
        },
      ]}
      sdkOptions={{
        gasSettings: { maxPriceInGwei: 500, speed: "fast" },
        readonlySettings: {
          chainId: ChainId.Mainnet,
          rpcUrl: "https://mainnet.infura.io/v3",
        },
        gasless: {
          openzeppelin: {
            relayerUrl: "your-relayer-url",
          },
        },
      }}
    >
      <YourApp />
    </ThirdwebProvider>
  );
};
4.1.8

5 months ago

4.1.7

6 months ago

4.1.9

5 months ago

3.16.1

7 months ago

3.16.0

7 months ago

3.16.3

7 months ago

3.16.2

7 months ago

3.16.5

7 months ago

3.16.4

7 months ago

4.1.4

6 months ago

4.1.3

6 months ago

4.1.6

6 months ago

4.1.5

6 months ago

4.1.0

6 months ago

4.1.2

6 months ago

4.1.1

6 months ago

4.0.5

7 months ago

4.0.4

7 months ago

4.0.7

7 months ago

4.0.6

7 months ago

4.0.1

7 months ago

4.0.0

7 months ago

4.0.3

7 months ago

4.0.2

7 months ago

3.14.33

9 months ago

3.14.34

9 months ago

3.14.35

8 months ago

3.14.36

8 months ago

4.0.9

6 months ago

3.14.37

8 months ago

4.0.8

6 months ago

3.14.38

8 months ago

3.14.39

8 months ago

3.14.30

9 months ago

3.14.31

9 months ago

3.14.32

9 months ago

3.14.40

8 months ago

3.14.41

8 months ago

3.14.19

10 months ago

3.14.11

10 months ago

3.14.12

10 months ago

3.14.13

10 months ago

3.14.14

10 months ago

3.14.15

10 months ago

3.14.16

10 months ago

3.14.17

10 months ago

3.14.18

10 months ago

3.14.10

10 months ago

3.14.22

9 months ago

3.14.23

9 months ago

3.14.24

9 months ago

3.14.25

9 months ago

3.14.26

9 months ago

3.14.27

9 months ago

3.14.28

9 months ago

3.14.29

9 months ago

3.14.20

10 months ago

3.14.21

9 months ago

3.15.0

8 months ago

3.14.9

10 months ago

3.14.8

10 months ago

3.14.7

10 months ago

3.14.6

10 months ago

4.1.10

5 months ago

4.0.10

6 months ago

3.14.3

10 months ago

3.14.5

10 months ago

3.14.4

10 months ago

3.14.2

10 months ago

3.14.1

10 months ago

3.14.0

11 months ago

3.13.1

11 months ago

3.11.9

12 months ago

3.11.11

12 months ago

3.11.10

12 months ago

3.13.0

11 months ago

3.12.1

12 months ago

3.12.0

12 months ago

3.12.3

11 months ago

3.12.2

12 months ago

3.12.4

11 months ago

3.11.8

1 year ago

3.11.4

1 year ago

3.11.3

1 year ago

3.11.6

1 year ago

3.11.5

1 year ago

3.11.7

1 year ago

3.11.0

1 year ago

3.11.2

1 year ago

3.11.1

1 year ago

3.10.1

1 year ago

3.10.0

1 year ago

3.10.3

1 year ago

3.10.2

1 year ago

3.9.3

1 year ago

3.9.2

1 year ago

3.9.1

1 year ago

3.9.0

1 year ago

3.9.5

1 year ago

3.9.4

1 year ago

3.7.4

1 year ago

3.7.3

1 year ago

3.7.2

1 year ago

3.8.2

1 year ago

3.8.1

1 year ago

3.7.1

1 year ago

3.6.9

1 year ago

3.6.8

1 year ago

3.6.7

1 year ago

3.6.11

1 year ago

3.6.10

1 year ago

3.7.0

1 year ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.6.2

1 year ago

3.6.1

1 year ago

3.6.0

1 year ago

3.6.6

1 year ago

3.6.5

1 year ago

3.6.4

1 year ago

3.6.3

1 year ago

3.5.2

1 year ago

3.5.1

1 year ago

3.5.0

1 year ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

3.0.0

2 years ago

2.8.1

2 years ago

2.8.0

2 years ago

2.9.2

2 years ago

2.9.1

2 years ago

2.9.4

2 years ago

2.9.3

2 years ago

2.9.6

2 years ago

2.9.5

2 years ago

2.9.7

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.8.2

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.7.5

2 years ago

2.9.0

2 years ago

2.8.0-1

2 years ago

2.8.0-0

2 years ago

2.7.3-5

2 years ago

2.7.3-6

2 years ago

2.7.3-1

2 years ago

2.7.3-2

2 years ago

2.7.3-3

2 years ago

2.7.3-4

2 years ago

2.7.3-0

2 years ago

2.7.0

2 years ago

2.7.0-0

2 years ago

2.7.0-1

2 years ago

2.7.0-2

2 years ago

2.7.1-0

2 years ago

2.7.1-1

2 years ago

2.7.1-2

2 years ago

2.4.3

2 years ago

2.4.2

2 years ago

2.4.5

2 years ago

2.4.4

2 years ago

2.6.0-3

2 years ago

2.6.0-4

2 years ago

2.6.0-1

2 years ago

2.6.0-2

2 years ago

2.6.0-0

2 years ago

2.4.3-0

2 years ago

2.5.0-0

2 years ago

2.6.1-0

2 years ago

2.6.5-0

2 years ago

2.4.7

2 years ago

2.4.6

2 years ago

2.4.8

2 years ago

3.0.0-1

2 years ago

3.0.0-0

2 years ago

2.4.2-4

2 years ago

2.4.2-3

2 years ago

2.4.2-2

2 years ago

2.4.2-1

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.6.3

2 years ago

2.6.2

2 years ago

2.6.2-0

2 years ago

2.6.6-0

2 years ago

2.4.9-0

2 years ago

2.4.9-1

2 years ago

2.4.9-4

2 years ago

2.4.9-2

2 years ago

2.4.9-3

2 years ago

2.5.0

2 years ago

2.5.1

2 years ago

2.6.3-0

2 years ago

2.6.3-1

2 years ago

2.6.3-4

2 years ago

2.6.3-5

2 years ago

2.6.3-2

2 years ago

2.6.3-3

2 years ago

2.6.5

2 years ago

2.6.4

2 years ago

2.2.7-0

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2-0

2 years ago

2.3.0

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.6

2 years ago

2.3.5

2 years ago

2.3.0-1

2 years ago

2.3.0-0

2 years ago

2.3.7-1

2 years ago

2.3.7-0

2 years ago

2.4.1-0

2 years ago

2.2.5-3

2 years ago

2.2.5-2

2 years ago

2.2.5-1

2 years ago

2.2.3

2 years ago

2.2.5-0

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.7

2 years ago

2.2.6

2 years ago

2.2.5-4

2 years ago

2.3.12-0

2 years ago

2.3.12-1

2 years ago

2.3.8

2 years ago

2.3.7

2 years ago

2.3.9

2 years ago

2.4.0-2

2 years ago

2.4.0-1

2 years ago

2.4.0-0

2 years ago

2.2.8-0

2 years ago

2.2.8-4

2 years ago

2.2.8-3

2 years ago

2.2.8-2

2 years ago

2.2.8-1

2 years ago

2.3.11-0

2 years ago

2.3.11-1

2 years ago

2.3.11-2

2 years ago

2.3.11-3

2 years ago

2.3.11-4

2 years ago

2.4.0-9

2 years ago

2.4.0-8

2 years ago

2.4.0-7

2 years ago

2.3.13

2 years ago

2.4.0-6

2 years ago

2.4.0-5

2 years ago

2.3.12

2 years ago

2.4.0-4

2 years ago

2.4.0-3

2 years ago

2.3.11

2 years ago

2.3.2-1

2 years ago

2.3.10

2 years ago

2.3.2-0

2 years ago

2.2.1

2 years ago

2.0.6-0

2 years ago

2.2.0

2 years ago

2.0.5

2 years ago

2.0.4-0

2 years ago

2.2.2

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.1.0-1

2 years ago

2.1.0-0

2 years ago

2.1.0-3

2 years ago

2.1.0-2

2 years ago

2.1.0-5

2 years ago

2.1.0-4

2 years ago

2.1.0-7

2 years ago

2.1.0-6

2 years ago

2.1.0-9

2 years ago

2.1.0-8

2 years ago

2.0.6-5

2 years ago

2.0.6-4

2 years ago

2.0.6-3

2 years ago

2.0.6-2

2 years ago

2.0.6-1

2 years ago

2.0.5-1

2 years ago

2.0.5-0

2 years ago

2.1.1-0

2 years ago

2.1.0

2 years ago

2.2.0-0

2 years ago

2.2.0-3

2 years ago

2.2.0-2

2 years ago

2.2.0-1

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.2-2

2 years ago

2.0.2-1

2 years ago

2.0.2-0

2 years ago

2.0.1-0

2 years ago

2.0.1

2 years ago

2.0.0-nightly.7

2 years ago

2.0.0-nightly.9

2 years ago

2.0.0-nightly.8

2 years ago

2.0.0-nightly.10

2 years ago

2.0.0-nightly.12

2 years ago

2.0.0-nightly.11

2 years ago

2.0.0-nightly.13

2 years ago

2.0.0

2 years ago

2.0.0-nightly.6

2 years ago

2.0.0-nightly.5

2 years ago

2.0.0-nightly.4

2 years ago

2.0.0-nightly.3

2 years ago

2.0.0-nightly.2

2 years ago

2.0.0-nightly.1

2 years ago

2.0.0-nightly.0

2 years ago

1.0.0-alpha.3

2 years ago

1.0.0-alpha.2

2 years ago

1.0.0-alpha.1

2 years ago