Nothing Special   »   [go: up one dir, main page]

Skip to content

Latest commit

 

History

History
 
 

sharing-loader-data

Sharing Loader Data

Sometimes you have data loaded in one route and you want to access that data in another route's component that's active on screen. You can do this via the useMatches hook.

Preview

Open this example on CodeSandbox:

Open in CodeSandbox

Example

This is a simple "workshop" app which has a "user" and a list of workshops. The user is loaded in the root loader and needed in the index route. The workshops are loaded in the workshops loader and then used in the individual workshop routes.

Related Links

Notes:

  • The app/routes/workshops/$workshopId.tsx route has a loader in it to determine whether the workshop exists. This is important and does not defeat the purpose of sharing the data. The loader runs on the server and we benefit by sharing the data because we don't have to send the data to the client.
  • A future version of Remix will provide an id on the matches which will make it easier to determine which match you want to access.
  • You could create a custom hook for accessing specific matches and add type assertion functions to ensure type safety rather than use as casting.