0

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;
0

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1
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`

Comments

0

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`;

Comments

0

You can use ES2015 template literals feature.

Template Literals MDN

`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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.