Reputation: 395
I have a function and i am trying to add the id parameter to the url and dynamically change the url and add the id parameter to it. but using curly braces doesnt reflect the parameter.
submitRating(rate, id) {
this.setState({
rating: rate,
});
console.log(this.state.rating);
console.log(id); ------> the console calls the id eg. id = 3499583
const url = "https://oversight-ws.herokuapp.com/api/politicians/{id}/rating" ---> that id should be rendered here on {id}
console.log(url); ---> i want to return this url https://oversight-ws.herokuapp.com/api/politicians/3499583/rating
return;
Upvotes: 0
Views: 1633
Reputation: 933
const id = 1
const url = "https://oversight-ws.herokuapp.com/api/politicians/" + id + "/rating"
or
const id = 1
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`
Upvotes: 1
Reputation: 238
You can use ES2015 template literals feature.
`string text ${expression} string text`
` => back-tick on keyboard
${} => placeholder within template
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`
===========================================================================
submitRating(rate, id) {
this.setState({
rating: rate,
});
console.log(this.state.rating);
console.log(id); ------> the console calls the id eg. id = 3499583
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating` ---> that id should be rendered here on {id}
console.log(url); ---> i want to return this url https://oversight-ws.herokuapp.com/api/politicians/3499583/rating
return;
Upvotes: 0
Reputation: 5202
Using ES6
and refer to this you van write it like this
const id = 3499583;
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
Upvotes: 0
Reputation: 8280
You are using es6
so you can use the string interpolation to do that :
const id = 3499583;
const url = `https://oversight-ws.herokuapp.com/api/politicians/${id}/rating`;
console.log(url);
Upvotes: 1