Socket
Book a DemoInstallSign in
Socket

@thirdweb-dev/react-core

Package Overview
Dependencies
Maintainers
10
Versions
3524
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thirdweb-dev/react-core

thirdweb React SDK <a href="htt

0.0.1-dev-60be95e
Source
npmnpm
Version published
Weekly downloads
2.3K
-83.95%
Maintainers
10
Weekly downloads
 
Created
Source



thirdweb React SDK

npm version Build Status Join our Discord!

Ultimate collection of React hooks for your web3 apps



Installation

You can install the SDK into your existing project using npm or yarn:

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

Getting Started

Our SDK uses a Provider Pattern; meaning any component within the ThirdwebSDKProvider will have access to the SDK.

Let's take a look at a typical setup:


Configure the ThirdwebSDKProvider

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

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

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

Note that you will need to provide an ethers provider or signer for you application. If you want further abstraction please see our react and react-native SDKs which have their own providers.

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

Create React AppNext.jsVite


Interact With Contracts

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

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

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-core";

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-core";

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-core";

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

FAQs

Package last updated on 26 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.