Comments (2)
I think this is a case of needing more documentation on the matter. As you have found, the route data functions only execute initially when the route is matched and won't run again until that route is un-matched and then matched again. This aligns with Solid's concept of components which similarly only run initially when created.
In order to handle the scenario you have, you'll want to leverage the reactive nature of the params
or location
properties passed into the function and derive your result. So in this case, instead of creating a signal you could use a memo or even a simple function. If you have async data needs, use a resource with the first argument based on the reactive values.
function FooData({ params }: { params: Params}): Accessor<number> {
return () => parseInt(params.id)
}
function FooData({ params }: { params: Params}): Accessor<number> {
return createMemo(() => parseInt(params.id))
}
function FooData({ params }: { params: Params}): { id: number } {
return {
get id() {
return parseInt(params.id)
}
}
}
function FooData({ params }: { params: Params}) {
const [resource] = createResource(() => parseInt(params.id), (id) => /* fetch some data */);
return resource;
}
from solid-router.
Got it. Ok yeah, looks like I'm still getting more familiar with the fundamentals of SolidJS. Brushing up on the documentation, this line now makes more sense:
Remember to access signals under a tracking scope if you wish them to react to updates. Tracking scopes are functions that are passed to computations like createEffect or JSX expressions.
If someone else stumbles upon this, I was using createResource
in my actual app, but I was not creating a tracking scope for my parameters. So I was doing this:
function FooData({ params }: { params: Params}) {
const [resource] = createResource(parseInt(params.id), (id) => /* fetch some data */);
return resource;
}
instead of this
function FooData({ params }: { params: Params}) {
const [resource] = createResource(() => parseInt(params.id), (id) => /* fetch some data */);
return resource;
}
Thanks @rturnq!
from solid-router.
Related Issues (20)
- How do I listen for changes to `location.pathname`. HOT 2
- If a function wrapped in action throws some error it should be possible to catch it. HOT 20
- README has reference to `element` which no longer exists.
- <A> Component causes incorrect error message when used outside of a <Route /> HOT 3
- [Bug] MemoryRouter - `set()` can't change the page
- `useLocation().state` does not get updated on history back HOT 1
- [Bug?]: Unable to use `ErrorBoundary` when throwing Error from cached function in combination with `load` HOT 5
- action always trigger on form submit
- Slow performance matching route with localeCompare HOT 2
- Route load results in inconsistent revalidation behaviour HOT 2
- Why not just export useRouter and useRoute? HOT 2
- Error: <A> and 'use' router primitives can be only used inside a Route. HOT 3
- SSR no matching exports HOT 1
- location.state undefined with HashRouter on v0.13.3 HOT 4
- useIsRouting returning true from SSR and causing Suspense issues HOT 2
- HTML anchors with unicode id hash could not trigger page jump HOT 2
- A component isActive subpath match incorrect
- `useCurrentMatches` is not reactive
- Crash when server function throws redirect and wrapped in createAsync HOT 3
- `window.location` holds incorrect value on initial client-side render HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from solid-router.