React: math.js seems to be not working my React calculator (using hooks) and producing React error #321

I’m trying to create a Calculator app with React using hooks and for calculating the total value I’m using math.js library, however it seems to be producing a Minified React error #321, which looks like this:

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app […].

I’m not sure why exactly it is happening since the function, where I have math.compile() and .evaluate() methods, is logging out correct string that is getting passed into math.compile() . Also when I tested this in plain JS file I got correct output:

const nums = '3+5*6-2/4'

const code1 = math.compile(nums)
const result = code1.evaluate()
console.log(result) // produces 32.5 as it is supposed to

But when the following function is called in the main React component on button click:

function handleEqual() {
  let totalRow = calcRow.slice(0,)

  if (regex.test(displayNum)) {
    if (displayNum === '-' && regex.test(calcRow[calcRow.length-1])) {
    totalRow = totalRow.slice(0, totalRow.length-1)
    totalRow = totalRow.slice(0, totalRow.length-1)
  } else {
    totalRow = totalRow.concat(displayNum)


  const total = math.compile(totalRow)
  const result = total.evaluate()
  // setCalcRow(totalRow + '=' + result)
  // setDisplayNum(result)
  // setTimeout(() => {
  //   setCalcRow(totalRow + '=' + result)
  //   setDisplayNum(result)
  // }, 500)


I get the aforementioned error. Why would it be happening and would could be done to get this issue fixed?

Here is my pen with the whole project code just in case.

Thanks in advance, Konstantin

console error image

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36.

Challenge: Build a JavaScript Calculator

Link to the challenge: