javascript - reactjs the syntaxError: url is readonly - Stack Overflow

i have this issue in react i have this function but its not right format of react check(img) {console.l

i have this issue in react i have this function but its not right format of react

check(img) {
        console.log(img,typeof img)
        const url="";
        const arrN = ["15","16","35","36","37","38","39","40","n15","n16","n35","n36","n37","n38","n39","n40"];
        for (var i = 0; i < arrN.length; i++) {
            if (img === arrN[i]) {
                 url = "/blah/allIcons/blah"+img+"_en.png";
            }else{
                 url = "/blah/allIcons/blah"+img+".png";
            }
        }
        return url;
    }

it give me this errror

Module build failed: SyntaxError: "url" is read-only

how i can do it ?

i have this issue in react i have this function but its not right format of react

check(img) {
        console.log(img,typeof img)
        const url="";
        const arrN = ["15","16","35","36","37","38","39","40","n15","n16","n35","n36","n37","n38","n39","n40"];
        for (var i = 0; i < arrN.length; i++) {
            if (img === arrN[i]) {
                 url = "/blah/allIcons/blah"+img+"_en.png";
            }else{
                 url = "/blah/allIcons/blah"+img+".png";
            }
        }
        return url;
    }

it give me this errror

Module build failed: SyntaxError: "url" is read-only

how i can do it ?

Share Improve this question asked Feb 9, 2017 at 12:30 marymary 191 silver badge8 bronze badges 3
  • 1 Maybe use have defined url above. just use url = "" instead of const url = ""; – Adnan Umer Commented Feb 9, 2017 at 12:32
  • @AdnanUmer Very bad advice. I guess you don't know why declaring variable is important. – dfsq Commented Feb 9, 2017 at 12:37
  • const is something that u can't change, use var or let, read the diff between const, var, let. stackoverflow./questions/762011/… – Mayank Shukla Commented Feb 9, 2017 at 12:52
Add a ment  | 

2 Answers 2

Reset to default 5

If you change url variable then it's should not be declared as a constant. Use let:

check(img) {
  const arrN = ["15", "16", "35", "36", "37", "38", "39", "40", "n15", "n16", "n35", "n36", "n37", "n38", "n39", "n40"];
  let url = "";

  for (var i = 0; i < arrN.length; i++) {
    if (img === arrN[i]) {
      url = "/blah/allIcons/blah" + img + "_en.png";
    } else {
      url = "/blah/allIcons/blah" + img + ".png";
    }
  }
  return url;
}

But you don't seem to need it anyway, as entire for-loop check seems inefficient. Can be optimized like this:

check(img) {
  const arrN = ["15", "16", "35", "36", "37", "38", "39", "40", "n15", "n16", "n35", "n36", "n37", "n38", "n39", "n40"];

  if (arrN.indexOf(img) > -1) { // or if (arrN.includes(img)) {...}
    return "/blah/allIcons/blah" + img + "_en.png";
  }

  return "/blah/allIcons/blah" + img + ".png";
}

Use let url="" instead of const url="".

The value of a constant cannot change through re-assignment, and it can't be redeclared.

So if you declare variable const url="", you can't later say url="/blah/allIcons/blah" + img + "_en.png"

  • const https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/const
  • let https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/let

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745404725a4626263.html

相关推荐

  • javascript - reactjs the syntaxError: url is readonly - Stack Overflow

    i have this issue in react i have this function but its not right format of react check(img) {console.l

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信