createResource not fetching on page change

Im using solid and solid router, I have a dynamic route I want to fetch data based on the id. When i go to a route with a different id it just shows the originally resource and not refetching. /category/1 --> change to /category/2 shows the data for 1.
const fetchLibraries = async (id: string) => {
const response = await fetch(
`http://localhost:4000/api/library/category/${id}`
);
return response.json();
};

function Category() {
const params = useParams();
const [libraries] = createResource(params.id, fetchLibraries);
return (
<For each={libraries()}>
{(library: Library) => (
<LibraryCard
name={library.name}
description={library.description}
repoURL={library.repoURL}
/>
)}
</For>
);
}

export default Category;
const fetchLibraries = async (id: string) => {
const response = await fetch(
`http://localhost:4000/api/library/category/${id}`
);
return response.json();
};

function Category() {
const params = useParams();
const [libraries] = createResource(params.id, fetchLibraries);
return (
<For each={libraries()}>
{(library: Library) => (
<LibraryCard
name={library.name}
description={library.description}
repoURL={library.repoURL}
/>
)}
</For>
);
}

export default Category;
3 Replies
dalton
daltonOP3y ago
const App: Component = () => {
return (
<Root>
<Sidebar />
<Content>
<Routes>
<Route path="/" component={Home} />
<Route path="/category/:id" component={Category} />
</Routes>
</Content>
</Root>
);
};
const App: Component = () => {
return (
<Root>
<Sidebar />
<Content>
<Routes>
<Route path="/" component={Home} />
<Route path="/category/:id" component={Category} />
</Routes>
</Content>
</Root>
);
};
Tommypop
Tommypop3y ago
Maybe try () => params.id instead of params.id as the resource source
dalton
daltonOP3y ago
That fixed it! Thank you

Did you find this page helpful?