solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.11k stars 138 forks source link

Run load functions with owner & context of `Router` component. #376

Closed Brendonovich closed 5 months ago

Brendonovich commented 5 months ago

It can be useful (and sometimes necessary) to access contexts declared above the <Router> inside loaders. In my case, we are preloading data using tRPC, which relies on useQueryClient to pull the tanstack query client out of context. This context provider is registered above the <Router>, ie. available at the top-level where loaders execute, but is currently inaccessible as load functions are not run in the context of any owner. To allow this to work, I've simply grabbed the router's owner and wrapped the loader invocation with runWithOwner. While this does work, I wouldn't be surprised if also wrapping the loader invocation with createRoot is necessary to account for some hooks that may read from context and create effects.

changeset-bot[bot] commented 5 months ago

🦋 Changeset detected

Latest commit: 052d3856bad1484c24e23bdf5b74cf49aab870a6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | --------------- | ----- | | @solidjs/router | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

ryansolid commented 5 months ago

Thanks. Regardless of other repercussions and downstream considerations this probably makes a lot of sense.