'passing a dynamic value in between get api url
How to pass a dynamic value in between get api url?
I have a ref that is connected to the input and want value of that input to be passed into a string. This does not work:
const location = useRef("Warsaw");
"http://api.weatherstack.com/current?&query={location.current.value}"
Solution 1:[1]
You have incorrect dynamic string syntax. You should use template literals:
const location = useRef("Warsaw");
let url = `http://api.weatherstack.com/current?&query=${location.current.value}`
Solution 2:[2]
You can use template literals
const location = useRef("Warsaw");
const uri = `http://api.weatherstack.com/current?&query=${location.current.value}`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sean Lawton |
| Solution 2 | OceanTechnic |
