You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/api/useController.md
+109-9Lines changed: 109 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,20 +49,120 @@ function MyComponent({ id }) {
49
49
50
50
## Examples
51
51
52
-
### Using the resolution
52
+
### Form submission
53
53
54
-
`controller.fetch()` matches the return type [useSuspense()](./useSuspense.md) - utilizing the [Endpoint.schema](/rest/api/RestEndpoint#schema)
55
-
when possible. This allows us to use any class members.
54
+
[fetch](./Controller.md#fetch) returns the denormalized response, matching [useSuspense()](./useSuspense.md)'s return type. This allows using Entity methods like `pk()`.
return <buttononClick={handleClear}>Refresh user data</button>;
113
+
}
114
+
```
115
+
116
+
:::tip
117
+
118
+
For better performance and consistency, prefer [including side effect updates in mutation responses](/rest/guides/side-effects).
119
+
120
+
:::
121
+
122
+
### Prefetching
123
+
124
+
Use [fetchIfStale](./Controller.md#fetchIfStale) to prefetch without overfetching fresh data.
125
+
126
+
```tsx
127
+
function ArticleLink({ id }: { id:string }) {
128
+
const ctrl =useController();
129
+
130
+
return (
131
+
<Link
132
+
to={`/article/${id}`}
133
+
onMouseEnter={() =>ctrl.fetchIfStale(ArticleResource.get, { id })}
134
+
>
135
+
Read more
136
+
</Link>
137
+
);
138
+
}
139
+
```
140
+
141
+
### Websocket updates
142
+
143
+
Populate cache with external data via [set](./Controller.md#set).
144
+
145
+
```tsx
146
+
function useWebsocket(url:string) {
147
+
const ctrl =useController();
148
+
149
+
useEffect(() => {
150
+
const ws =newWebSocket(url);
151
+
ws.onmessage=event=> {
152
+
const { entity, args, data } =JSON.parse(event.data);
153
+
ctrl.set(EntityMap[entity], args, data);
154
+
};
155
+
return () =>ws.close();
156
+
}, [ctrl, url]);
157
+
}
158
+
```
159
+
160
+
:::warning
161
+
162
+
For production use, implement a [Manager for data streams](../concepts/managers.md#data-stream) rather than component-level effects. Managers handle connection lifecycle globally and work with SSR.
0 commit comments