Data APIs

Response helpers

Edit this page

Note: These response helpers must be called within a cache or action function. If you want to programmatically navigate, you will want to use the useNavigate primitive.


Redirect

Signature: redirect(path, options)

Redirects to the next route.

const getUser = query(() => {
const user = await api.getCurrentUser();
if (!user) throw redirect("/login");
return user;
})

Reload

Signature: reload(options)

Reloads the data at the given cache key on the current route.

const getTodo = query(async (id: number) => {
const todo = await fetchTodo(id);
return todo;
}, "todo");
const updateTodo = action(async (todo: Todo) => {
await putTodo(todo.id, todo);
return reload({ revalidate: getTodo.keyFor(id) });
});

json

Signature: json(data, options)

Returns JSON data from an action while also providing options for controlling revalidation of cache data on the route.

const getTodo = query(async (id: number) => {
const todo = await fetchTodo(id);
return todo;
}, "todo");
const getCompletedTodos = action(async () => {
const completedTodos = await fetchTodo({ status: 'complete' });
return json(completedTodos, { revalidate: getTodo.keyFor(id) });
});
Report an issue with this page