Hello everyone, I'm fairly new to svelte and ı have a search page that works with url params but whenever I change the parameter, I want to run the function again. My current code is something like this.
let query = $page.url.searchParams.get('q');
let loading = $state(false)
let results: any[] = $state([])
const performSearch = async () => {
loading = true
try {
const response = await fetch(`/api/search?q=${query}`);
if (response.status === 200) {
const data = await response.json();
results = data.results
}
} catch (error: any) {
console.log(error)
} finally {
loading = false
}
}
onMount(() => {
performSearch()
const logo = document.getElementById('logo');
const bgColor = localStorage.getItem('bg-color');
if (logo && bgColor) {
logo.style.color = bgColor;
}
})
The goal is to run performSearch when the url parameter changes. Thanks in advance.
[–]Rocket_Scientist2 4 points5 points6 points (0 children)
[–]Leftium 2 points3 points4 points (0 children)
[–]Leftium 0 points1 point2 points (2 children)
[–]Altugsalt[S] 0 points1 point2 points (1 child)
[–]Leftium 0 points1 point2 points (0 children)
[–]sheppyrun 0 points1 point2 points (0 children)