0

Hello I doing project web3 and i use wagmi. When i get listTokenUrl, after i want call each item to read tokenUri, but have error "Error: React Hook "useContractRead" may be executed more than once. Possibly because it is called in a loop. React Hooks must be called in the exact same order in every component render. react-hooks/rules-of-hooks"

There my code, tks so much .

I get list tokenID here


  const { data: listTokenIdBaby } = useContractBabyListTokenIds({
    args: address ? [address] : undefined,
  });

After i map lisToken to get tokenUri

  const dataBaby = listTokenIdBaby?.map(item => {
    const { data } = useContractBabyTokenUri({
      args: [item],
    });
    return data;
  });

1 Answer 1

0

Looks like you might want to use the useContractReads hook. (useContractReads)

You would want to map through listTokenIdBaby and pass the config array as contracts: ... to useContractReads.

An example from my personal experience is below:

const { data: walletClient } = useWalletClient();
  const { isLoading, data: availableGameSessions } = useContractRead({
    ...contracts.factory,
    functionName: 'getGameSessions',
    account: walletClient?.account,
    watch: true,
  });
  const { data: activeGameSessions, isLoading: isGameStakesLoading } =
    useContractReads({
      contracts: availableGameSessions?.map((gameSession: Hash) => {
        return {
          address: gameSession,
          functionName: 'stake',
          abi: contracts.rpslsGame.abi,
        };
      }),
      select: (data) => {
        if (!availableGameSessions) return [];

        return data
          .filter((session) => Number(session.result) > 0)
          .map((_, index) => availableGameSessions[index]);
      },
      watch: true,
    });

If you provide a more detailed reproduceable template, I can help you adapting the hooks calls for your specific purposes.

2
  • 1
    tks sir, i will try it, i doing use cli like here i dont know can change here, i think will use ur solution. Commented Nov 4, 2023 at 9:37
  • 1
    export function useContractBooksListTokenIds< TFunctionName extends 'listTokenIds', TSelectData = ReadContractResult<typeof contractBooksABI, TFunctionName>, >( config: Omit< UseContractReadConfig<typeof contractBooksABI, TFunctionName, TSelectData>, 'abi' | 'address' | 'functionName' > = {} as any ) { return useContractRead({ abi: contractBooksABI, address: contractBooksAddress, functionName: 'listTokenIds', ...config, } as UseContractReadConfig< typeof contractBooksABI, TFunctionName, TSelectData >); } Commented Nov 4, 2023 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.