Home > OS >  UseEffect return value
UseEffect return value

Time:05-19

I have a function that basically executes a backend function to return an integer.

I'm basically executing this:

  export async function CheckSupply() {
    const near = await connect(getConfig);
    const account = await near.account("myaddress");
    const supply = await account.viewFunction(
        "myaddress", 
        "nft_total_supply", 
        {}
    )
  }

using

import { CheckSupply } from "./utils";

    useEffect(() => {
     CheckSupply();
    },[]);
    

This seems to run fine, but I can't seem to get the value that is being returned from the function and not sure why?

Any help would be amazing.

Thanks

CodePudding user response:

Your CheckSupply doesn't currently have a return statement. I'm assuming the value you care about is supply, so you would do:

  export async function CheckSupply() {
    const near = await connect(getConfig);
    const account = await near.account("myaddress");
    const supply = await account.viewFunction(
        "myaddress", 
        "nft_total_supply", 
        {}
    )
    return supply; // <----- added
  }

You also don't have any code that uses the return value, so you'll need to add that. Note that since CheckSupply is an async function, it will be returning a promise, so you'll need to await that promise to interact with the eventual supply value:

useEffect(() => {
  const load = async () => {
    const supply = await CheckSupply();
    // Do something with `supply`. Maybe set state?
  };
  load();
}, [])
  • Related