Skip to main content

useController()

Controller provides type-safe methods to manipulate the store.

For instance fetch, invalidate, and setResponse

import { useController } from '@rest-hooks/react';

function MyComponent({ id }) {
const ctrl = useController();

const handleRefresh = useCallback(
async e => {
await ctrl.fetch(MyResource.get, { id });
},
[fetch, id],
);

const handleSuspend = useCallback(
async e => {
await ctrl.invalidate(MyResource.get, { id });
},
[invalidate, id],
);

const handleLogout = useCallback(
async e => {
ctrl.resetEntireStore();
},
[resetEntireStore],
);
}

Examples

/next

@rest-hooks/react/next contains the version of useController() that will ship with the next version. This provides a return value that matches useSuspense() - utilizing the Endpoint.schema

import { useController } from '@rest-hooks/react/next';

const post = await controller.fetch(PostResource.create, createPayload);
post.title;
post.pk();

Todo App

Explore more Rest Hooks demos