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 useurl = ""
instead ofconst 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
2 Answers
Reset to default 5If 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
评论列表(0条)