DEV Community

Discussion on: Svelte 3: Usando blocos "if"

Collapse
 
blindfish3 profile image
Ben Calder

Cool to see people writing Svelte articles :)

I noticed your code has a bug though:
Using += to mutate API_URL is probably not what you intended:

let API_URL = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=";
function dataSubmit(e) {
  if (inputText !== "") {
    textPresent = true;
    API_URL += inputText;
    // look at the result after making multiple submissions to see why this is bad:
    console.log(API_URL);
  }
}
Enter fullscreen mode Exit fullscreen mode

Instead you can simply make API_URL a const and append the input to it:

// this shouldn't ever change!
const API_URL = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=";
function dataSubmit(e) {
  if (inputText !== "") {
    loadingQrCode = true;
    let img = new Image();
    img.src = API_URL + inputText; // better ;)
    console.log(img.src);
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oieduardorabelo profile image
Eduardo Rabelo

hey Been, thanks for spotting that! Since this is a 1:1 translation, I usually don't review the code example, and seems I should've done to this one! :D I appreciate your effort, cheers

Collapse
 
oieduardorabelo profile image
Eduardo Rabelo

it is fixed, thanks mate!