Skip to content

Instantly share code, notes, and snippets.

@djedr
Last active June 9, 2023 18:24
Show Gist options
  • Save djedr/4cfaa5ecd1e139e9111de8bbf03ca9a3 to your computer and use it in GitHub Desktop.
Save djedr/4cfaa5ecd1e139e9111de8bbf03ca9a3 to your computer and use it in GitHub Desktop.
const smartQueryString = ((strs, ...params) => {
let ret = ''
for (let i = 0; i < strs.length - 1; ++i) {
const str = strs[i]
const param = params[i]
// or whatever escaping method is appropriate
ret += str + encodeURIComponent(param)
}
return ret
})
const sqs = smartQueryString
// only thing changed now is sqs prepended to the string
fetch(sqs`/api/cities?country=${country}`)
.then(response => response.json())
.then(json => {
if (!ignore) {
setCities(json);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment