为什么 Web3 .GetReserves() 向我的控制台发送垃圾邮件?

Why Web3 .GetReserves() is spamming my console?

我下面的代码正确获取了数据;但是,当我尝试获取令牌的储备时,会出现一个问题。它一遍又一遍地发送垃圾邮件,以至于我无法在短时间内请求数据。 (希望我一个人不会破坏 RPC 端点)。

代码如下[next.js/react]

export default function Chart(props) {
  const [pairAddress, setPairAddress] = useState(null);
  const [reserves, setReserves] = useState(null);
  console.log(pairAddress);
  const userAddy = props.Token;
  const decimals = props.Decimals;
  const Web3 = require('web3');
  const urlBsc = 'https://bsc-dataseed.binance.org';
  var web3Bsc = new Web3(urlBsc);
  let myContractPCSF = null;
  let myContractPCSR = null;
  const pancakeFactory = '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73'
  var abiPCSF = [{
    "constant": true,
    "inputs": [{
        "internalType": "address",
        "name": "",
        "type": "address"
      },
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "name": "getPair",
    "outputs": [{
      "internalType": "address",
      "name": "",
      "type": "address"
    }],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  }]
  var abiPCFR = [{
    "constant": true,
    "inputs": [],
    "name": "getReserves",
    "outputs": [{
        "internalType": "uint112",
        "name": "_reserve0",
        "type": "uint112"
      },
      {
        "internalType": "uint112",
        "name": "_reserve1",
        "type": "uint112"
      },
      {
        "internalType": "uint32",
        "name": "_blockTimestampLast",
        "type": "uint32"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  }]
  if (userAddy.length == 42 && userAddy[0] == '0' && userAddy[1] == 'x') {
    myContractPCSF = new web3Bsc.eth.Contract(abiPCSF, pancakeFactory, {
      from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', // default from address
      gasPrice: '45031112648' // default gas price in wei
    });
    myContractPCSR = new web3Bsc.eth.Contract(abiPCFR, pairAddress, {
      from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', // default from address
      gasPrice: '45031112648' // default gas price in wei
    })
    async function getContData() {
      let PCSFcontract = await myContractPCSF;
      let PCSRcontract = await myContractPCSR;
      PCSFcontract.methods.getPair(userAddy, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c').call(function(error, result) {
        if (error) {
          console.log(error);
        }
        if (result != '0x0000000000000000000000000000000000000000') {
          setPairAddress(result)
          PCSRcontract.methods.getReserves().call(function(error, result) {
            if (error) {
              console.log(error);
            }
            if (result) {
              setReserves(result)
            }
          })
        }
        if (result == '0x0000000000000000000000000000000000000000') {
          PCSFcontract.methods.getPair(userAddy, '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56').call(function(error, result) {
            if (error) {
              console.log(error);
            }
            if (result) {
              setPairAddress(result)
            }
          })
        }
      })
    }
    getContData()
  }
  return ( <
    div id = {
      styles.ChartWrapper
    } >
    </div>
  )
}

我做错了什么?

经过一些重新思考和测试后,我注意到我的状态发生了变化,导致了无限循环。

我几秒钟前刚刚学会了如何使用 UseEffect,它解决了我的问题。