npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-near

v6.1.2

Published

<h1 align="center">react-near</h1>

Downloads

113

Readme

Introduction

Quick implementation of near in your application. Generation of typed smart contract methods. Including ready for use typed methods in popular smart contract Standards.

Navigation

Setup

You'll need to install the package from npm npm i react-near near-api-js.

If you need to generate contract methods npm i -D json-schema-to-typescript fzstd.

Quick Example

// config.ts
const FT_CONTRACT_NAME = 'mfight-ft.testnet';

// app.tsx
function MyApp({ Component, pageProps }: AppProps) {
   return (
      <NearEnvironmentProvider defaultEnvironment={NearEnvironment.TestNet}>
         <NearProvider authContractId='my-contract.testnet'>
            <Component {...pageProps} />
         </NearProvider>
      </NearEnvironmentProvider>
   );
}

// page.tsx
function Page() {
   const nearUser = useNearUser();

   const { data: ftBalance = '0', refetch: refetchFtBalance } = useFtBalanceOf({
      contract: 'mfight-ft.testnet',
      variables: { account_id: nearUser.address as string },
      poolInterval: 1000 * 60 * 5,
      skip: !nearUser.isConnected,
   });
   const [transferCall] = useFtTransferCall({
      contract: 'mfight-ft.testnet',
      gas: GAS_FOR_FT_TRANSFER_CALL,
   });

   const handleTransferCall = () =>
      transferCall(
         { receiver_id: 'example.near', amount: 1000, msg: JSON.stringify({}) },
         parseNearAmount('0.01'),
      );
   const handleSignIn = () => nearUser.connect();

   return (
      <div>
         {nearUser.isConnected && (
            <>
               <span>NEAR balance: {nearUser.balance}</span>
               <span>FT balance: {formatNearAmount(ftBalance, 24)}</span>
               <button onClick={handleTransferCall}>transfer ft</button>
            </>
         )}
         {!nearUser.isConnected && <button onClick={receiver_id}>Connect NEAR</button>}
      </div>
   );
}

Codegen

If you want to generate all the methods of your near contract, you have to:

  • you need to add the abi feature, as in this example abi-example
  • run npm i -D json-schema-to-typescript fzstd
  • add config to your project react-near.json
{
   "dist": "near-api",
   "type": "default", // or "raw", for exclude all hooks
   "contracts": [
      {
         "name": "Nft",
         "abi": "abi/nft.json",
         "mainnet": "mfight-nft.near",
         "testnet": "mfight-nft_v2.testnet"
      },
      {
         "name": "Market",
         "mainnet": "mfight-nft.near",
         "testnet": "mfight-nft_v2.testnet"
      }
   ]
}
  • run script node ./node_modules/react-near/codegen.js

There is also an example: example-app

Api

NearProvider

Create a context of NEAR in the application

// app.tsx
function MyApp({ Component, pageProps }: AppProps) {
   return (
      <NearProvider authContractId='my-contract.testnet'>
         <Component {...pageProps} />
      </NearProvider>
   );
}
function Page() {
   const near = useNear();
   const nearUser = useNearUser();
   const nearAccount = useNearAccount(nearUser.address);

   return (
      <>
         <span>User Address: {account.address}</span>
         <span>Balance: {nearAccount?.balance} NEAR</span>
      </>
   );
}

NearEnvironmentProvider

The state of ENV will be saved in a cookie

// app.tsx
function MyApp({ Component, pageProps }: AppProps) {
   return (
      <NearEnvironmentProvider defaultEnvironment={NearEnvironment.TestNet}>
         <Component {...pageProps} />
      </NearEnvironmentProvider>
   );
}
function Page() {
   const nearEnv = useNearEnv();

   const handleChangeEnv = () => nearEnv.update(NearEnvironment.TestNet);

   return (
      <>
         <span>Current Env: {nearEnv.value}</span>
         <button onClick={handleChangeEnv}>switch to testnet</button>
      </>
   );
}

useNearUser

Everything you need to manage your account

function Page() {
   const nearUser = useNearUser();

   return (
      <>
         <button onClick={() => nearUser.connect()}>Connect wallet</button>
         <button onClick={() => nearUser.disconnect()}>Disconnect wallet</button>

         <span>Is Connected: {nearUser.isConnected}</span>
         <span>Address: {nearUser.address}</span>
      </>
   );
}

Batch transactions

For example, if you need to transfer 2 coins into a liquidity pool in single call

const MT_CONTRACT_ID = 'mt-token.testnet';
const FT_CONTRACT_ID_1 = 'ft-token-one.testnet';
const FT_CONTRACT_ID_2 = 'ft-token-two.testnet';
const POOL_CONTRACT_ID = 'two-tokens-receiver.testnet';

function Page() {
   const nearUser = useNearUser();

   const [ftTransferCall1, ftTransferCallCtx1] = useFtTransferCall({
      contract: 'mfight-ft.testnet',
      gas: GAS_FOR_FT_TRANSFER_CALL,
   });
   const [ftTransferCall2, ftTransferCallCtx2] = useFtTransferCall({
      contract: 'mfight-ft.testnet',
      gas: GAS_FOR_FT_TRANSFER_CALL,
   });
   const [mtBatchTransferCall, mtTransferCallCtx] = useMtBatchTransferCall({
      contract: 'mfight-ft.testnet',
      gas: GAS_FOR_MT_TRANSFER_CALL,
   });

   const handleTransfer = async () => {
      const amount1 = parseNearAmount('1') as string;
      const amount2 = parseNearAmount('1') as string;

      return nearUser.signAndSendTransactions({
         transactions: [
            {
               signerId: nearUser.address as string,
               receiverId: MT_CONTRACT_ID,
               actions: [
                  {
                     type: 'FunctionCall',
                     params: {
                        methodName: 'ft_transfer',
                        args: btoa(JSON.stringify({ receiver_id: 'xx', amount: '1' })),
                        gas: 'xxx',
                        deposit: '1',
                     },
                  },
               ],
            },
         ],
      });
   };

   return (
      <>
         <button onClick={handleTransfer}>Batch Transactions</button>
      </>
   );
}

useNearQuery

Calling the view method from the contract

function Page() {
   const nearUser = useNearUser();

   const {
      data: balance = '0', // method result
      loading, // waiting for result or error
      error, // error, if exists
      refetch, // refresh state
   } = useNearQuery<string, { account_id: string }>('ft_balance_of', {
      contract: 'mfight-ft.testnte', // contract of method
      variables: {
         account_id: 'account.near',
      }, // arguments of method
      poolInterval: 1000 * 60, // refresh state with interval
      skip: !nearUser.isConnected, // method will not be called
      debug: true, // debug method, print info to console
      onError: (err) => console.log(err), // error handler
      onCompleted: (res) => console.log(res), // result handler
   });

   return (
      <>
         <span>Balance: {formatNearAmount(balance)}</span>
         {loading && <span>Loading...</span>}
         {error && <span>Error: {error}</span>}
         <button onClick={() => refetch({}).catch(console.log)}>Force Refresh</button>
      </>
   );
}

useNearMutation

Calling the change method from the contract

function Page() {
   const nearUser = useNearUser();

   const [transfer, { loading, data }] = useNearMutation<
      string,
      { receiver_id: string; amount: string }
   >('ft_transfer', {
      gas: NEAR_GAS, // gas for this method
      contract: 'mfight-ft.testnet',
      debug: true, // debug method, print info to console
      onError: (err) => console.log(err), // error handler
      onCompleted: (res) => console.log(res), // result handler
   });

   return (
      <>
         <span>Result: {data}</span>
         {loading && <span>Loading...</span>}
         <button
            onClick={() =>
               transfer(
                  { amount: parseNearAmount('10'), receiver_id: 'account.near' },
                  parseNearAmount('0.01') as string,
               ).catch(console.log)
            }
         >
            Tranfer
         </button>
      </>
   );
}

useNearStatus

Get result transactions hashes if method is success, or get error if failure

function Page() {
   const { transactionHashes, error } = useNearStatus();

   return (
      <>
         {transactionHashes && <span>Success Tx List: {transactionHashes}</span>}
         {error && <span>{error}</span>}
      </>
   );
}

Contracts

NFT

  • [x] nft_metadata
  • [x] nft_token
  • [x] nft_transfer
  • [x] nft_transfer_call
  • [x] nft_tokens
  • [x] nft_tokens_for_owner
  • [x] nft_total_supply
  • [x] nft_supply_for_owner
  • [x] nft_is_approved
  • [x] nft_approve
  • [x] nft_revoke
  • [x] nft_revoke_all
  • [x] nft_payout
  • [x] nft_transfer_payout

FT

  • [x] ft_metadata
  • [x] ft_transfer
  • [x] ft_transfer_call
  • [x] ft_balance_of
  • [x] ft_total_supply

MT

  • [x] mt_balance_of
  • [x] mt_total_supply
  • [x] mt_batch_transfer
  • [x] mt_batch_transfer_call

Storage

  • [x] storage_balance_of
  • [x] storage_balance_bounds
  • [x] storage_deposit
  • [x] storage_withdraw
  • [x] storage_unregister

Paras

  • [x] nft_approve (useParasNftApprove)
  • [x] get_market_data (useParasMarketData)
  • [x] api collection-stats (useParasCollectionStats)

Ref Finance

  • [x] get_pool (useRefFinancePool)

Utils

  • [x] parseNearAmount with decimals
  • [x] isValidNearAddress
  • [x] parseNearDate
  • [x] formatNearAddress
  • [x] gas and more :)

Authors