html - Concatenating variables and strings in React -
is there way incorporate react's curly brace notation , href
tag? have following value in state:
{this.state.id}
and following html attributes on tag:
href="#demo1" id="demo1"
is there way can add id
state html attribute this:
href={"#demo + {this.state.id}"}
which yield:
#demo1
you're correct, misplaced few quotes. wrapping whole thing in regular quotes literally give string #demo + {this.state.id}
- need indicate variables , string literals. since inside {}
inline jsx expression, can do:
href={"#demo" + this.state.id}
this use string literal #demo
, concatenate value of this.state.id
. can applied strings. consider this:
var text = "world";
and this:
{"hello " + text + " andrew"}
this yield:
hello world andrew
you can use es6 string interpolation/template literals ` (backticks) , ${expr}
(interpolated expression), closer seem trying do:
href={`#demo${this.state.id}`}
this substitute value of this.state.id
, concatenating #demo
. equivalent doing: "#demo" + this.state.id
.
Comments
Post a Comment