Uniswap / redux-multicall

A React + Redux library for fetching, batching, and caching chain state via the MultiCall contract.
MIT License
94 stars 53 forks source link

Bad setState call inside Updater #23

Open warneracw21 opened 2 years ago

warneracw21 commented 2 years ago

I am receiving an error when attempting to use Multicall Updater similar to the implementation in uniswap-interface

import React from 'react'
import { combineReducers, createStore } from 'redux'

import { createMulticall } from '@uniswap/redux-multicall'
import { useWeb3React } from '@web3-react/core'
import { SupportedChainId } from '../constants/chains'
import { useInterfaceMulticall } from '../hooks/useContract'
import useBlockNumber from '../contexts/BlockNumberContext'

const multicall = createMulticall()
const reducer = combineReducers({ [multicall.reducerPath]: multicall.reducer })

export default multicall

function getBlocksPerFetchForChainId(chainId): number {
  switch (chainId) {
    case SupportedChainId.RINKEBY:
    case SupportedChainId.ROPSTEN:
      return 5
    default:
      return 1
  }
}

export function MulticallUpdater() {
  const { chainId } = useWeb3React()
  const latestBlockNumber = useBlockNumber()
  const contract = useInterfaceMulticall()

  const listenerOptions = React.useMemo(
    () => ({
      blocksPerFetch: getBlocksPerFetchForChainId(chainId),
    }),
    [chainId]
  )

  return (
    <multicall.Updater
      chainId={chainId}
      latestBlockNumber={latestBlockNumber}
      contract={contract}
      listenerOptions={listenerOptions}
    />
  )
}
 Warning: Cannot update a component (`Updater`) while rendering a different component (`Updater`)

Stack Trace

Warning: Cannot update a component (`Updater`) while rendering a different component (`Updater`). To locate the bad setState() call inside `Updater`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at Updater
    at UpdaterContextBound
    at MulticallUpdater 
    at Updaters
    at BlockNumberProvider
    at Web3ReactProvider 
    at UserPreferencesProvider
    at Provider

Versions

node v16.8.10
react v18.2.0
HeymanGG commented 2 years ago

I am having the same problem. If you remove the listenerOptions the warning goes away

warneracw21 commented 2 years ago

Is there a way to set the blocksPerFetch without the listenerOptions?