skip to Main Content

I have following code:

if (ev.data.type === "height") {
        if (ev.data.form === src) {
          setHeight(ev.data.message + "px");
        }
      }

which by default add height 1900px and now I want to add additional 10px so it became 1910px How I can achieve this using parseInt and so I can modify my above syntax?

Thanks in advance!

2

Answers


  1. Assuming that ev.data.message is a string that’s coercable to a Number type, then this will work:

    setHeight((parseInt(ev.data.message, 10) + 10) + "px");
    

    More information on parseInt() is available at MDN

    Login or Signup to reply.
  2. if (ev.data.type === "height") {
      if (ev.data.form === src) {
        const msg = parseInt(ev.data.message, 10);
        if (typeof msg === "number" && msg !== NaN) {
          setHeight(msg + 10 + "px");
        }
      }
    }
    

    Firstly transform the message to the int, and check the type and value.
    If it’s ok, go ahead to set new height.

    Check the parseInt out in the MDN

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search