Hi, I've been confused by this error a bit whilst writing my application and I have just constructed a minimal reproducible example and now it clicks. But I'd be interested in knowing what I can do to avoid this problem in the future:
import { RouteSectionProps, useLocation, useParams } from "@solidjs/router";
import { Component } from "solid-js";
export const TestPage: Component<RouteSectionProps<unknown>> = (props) => {
return (
<b>
This is a test page! <SubComponent path={useLocation().pathname} />
</b>
);
};
const SubComponent: Component<{ path: string }> = (props) => (
<button
type="button"
style="border: 1px solid blue; padding: 3em;"
onClick={() => console.log(props.path)}
>
Clicky!
</button>
);
Clicking the button produces the error (whilst evaluating props.path): '<A> and 'use' router primitives can be only used inside a Route.'
My understanding of the problem here: presumably the event handler onClick runs outside of a tracking scope, so useLocation(), which is invoked lazily because of props.path actually just being a property getter, is not run with the router in scope and so it doesn't know what to return.
Although I can usually figure out how to eagerly evaluate props.path if I put my mind to it, I am struggling to fix this in a way that ensures I don't keep making this mistake again and again, any ideas?
Thanks