Search code examples
javascriptreactjsstateeval

Why do I get 'implied eval. Consider passing a function instead of a string' when passing a simple string to state?


I'm just making a really simple calculator and setting the return text so that it prints on the screen for the user. It is really simple stuff, and I've passed strings to state heaps of times. Why am I getting this error?

Here's the relevant code block: (see comments for line numbers which line up with the error message

    crcl = (((140 - age) * idbw) / (0.814 * creatinine)) * cockcroftFactor
    crclRounded = Math.round(crcl)
    setCreatinineClearance(crclRounded);
    if (crcl < 20 && weight >= 40 && weight <= 110) {
      setInterval("48-hourly, take trough level before the first dose");
    } else if (crcl >= 20 && crcl <= 60 && weight >= 40 && weight <= 110) {
      setInterval("24-hourly, take trough level before the third dose"); //this is line 131
    } else if (crcl >= 60 && weight >= 40 && weight <= 110) {
      setInterval("12-hourly, take trough level before the fourth dose"); //this is line 133
    }
    if (weight < 40) { //this is line 135
            alert("Please contact infectious diseases for dosing in underweight patients");
            closeDialog();
            return;
        } else if ((weight >= 40) && (weight <= 49)) {
            setDose("750")
        } else if ((weight >= 50) && (weight <= 64)) {
            setDose("1000")
        } etc.....

Here's the error message: enter image description here


Solution

  • EDIT: Right, from @Etheryte's comment, I realized you were intending setInterval to be the setState function name. I completely missed that. Since setInterval is a name for a function that already exists, I guess you just need to use a different name for your setState function.


    You can pass a function, not a string, to setInterval to make the browser (or nodejs) execute the passed function at a given interval that you pass as the second argument. You have passed neither.

    I assume the reason it says "implied eval" is that you passed a string, and thinks you expect it to parse the string into javascript and evaluate it as js, which is unsafe, and it refuses to do so.

    Below is an example of using setInterval.

    setInterval(() => {
      console.log("Hello!")
    }, 1000)