Excellent question! This is a common point of confusion for developers coming from older SolidJS examples or tutorials.
Yes, the data property on the RouteDefinition object is effectively deprecated and has been replaced by a more modern and powerful pattern.
The current and recommended way to load data for a route is to export a routeData function from the route's component file itself.
Let's break down the old way, the new way, and why the change was made.
1. The Old Way (Deprecated): data Property
In older versions of solid-router, you would define your data-fetching logic directly within the route configuration array, like this:
src/routes.js (Old Pattern)
import { lazy } from 'solid-js';
import { RouteDefinition } from '@solidjs/router';
const fetchUserData = async ({ params }) => {
const response = await fetch(`https://api.example.com/users/${params.id}`);
return response.json();
};
export const routes: RouteDefinition[] = [
{
path: '/users/:id',
component: lazy(() => import('./pages/UserProfile')),
// This `data` property is the deprecated pattern
data: fetchUserData
},
// ...other routes
];
In the component, you would then access this data via props:
src/pages/UserProfile.jsx (Old Pattern)
import { For } from 'solid-js';
// The data was passed as a prop
function UserProfile(props) {
return (
<div>
<h1>{props.data.name}</h1>
<p>Email: {props.data.email}</p>
</div>
);
}
export default UserProfile;
This pattern worked, but it had several drawbacks, which led to its replacement.
2. The New & Current Way: Exported routeData Function
The modern approach, heavily inspired by file-based routing and frameworks like Remix, is to co-locate the data-loading function with the component that uses the data.
You export a function named routeData from your route's component file. solid-router will automatically discover and execute this function when navigating to the route.
src/routes/users/[id].jsx (Modern Pattern)
(Note: This example uses file-based routing, which is common with this pattern, but it works with manual route configuration too.)
import { createResource } from 'solid-js';
import { useRouteData, useParams } from '@solidjs/router';
// 1. The Data Loading Function (exported)
export function routeData() {
const params = useParams();
const [user] = createResource(() => params.id, async (id) => {
// A 1-second delay to simulate network latency
await new Promise(r => setTimeout(r, 1000));
const response = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
if (!response.ok) {
throw new Error("Failed to fetch user");
}
return response.json();
});
return user;
}
// 2. The Component
export default function UserProfile() {
// 3. Access the data with the `useRouteData` hook
const user = useRouteData();
return (
<main>
<h1>User Profile</h1>
<Show when={user()} fallback={<p>Loading user...</p>} keyed>
{(data) => (
<div>
<h2>{data.name} (@{data.username})</h2>
<p><strong>Email:</strong> {data.email}</p>
<p><strong>Website:</strong> {data.website}</p>
</div>
)}
</Show>
<Show when={user.error}>
<p style={{ color: 'red' }}>Error: {user.error.message}</p>
</Show>
</main>
);
}
Key Differences Summarized
| Feature | Old Way (data property) | New Way (routeData export) |
|---|
| Location | In the central routes.js file. | In the component file (UserProfile.jsx). |
| Co-location | Poor. Logic is separate from UI. | Excellent. Data logic lives with the component. |
| Data Access | Via props.data. | Via the useRouteData() hook. |
| Modularity | Tightly coupled to route configuration. | Decoupled and highly modular. |
| Type Safety | Harder to type correctly. | Much easier to infer types between routeData and useRouteData. |
| Lazy Loading | Less efficient. The data function might be bundled with all route definitions. | More efficient. The data function is loaded only when the component is. |
Why Was the Change Made?
The shift to the routeData export was a significant improvement for several reasons:
- Co-location: Keeping data-fetching logic next to the component that consumes it dramatically improves maintainability and makes the code easier to reason about.
- Modularity & Reusability: Each route becomes a self-contained unit.
- Better Tooling & DX: This pattern works seamlessly with file-system-based routing (like in SolidStart) and allows for better type inference.
- Performance: It aligns perfectly with lazy loading. The
routeData function and the component are loaded as a single chunk only when the user navigates to that route.
In conclusion, if you see the data property in a RouteDefinition, you are looking at an outdated pattern. You should always use the exported routeData function and the useRouteData hook in modern SolidJS applications.