Simplify websocket hooks initialization
- Dominant language
- TypeScript
- Stars
- 0
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
Update: after discussion we can actually both respect the rules of hooks and reduce the boiler plate, by simply considering the `enableWebsocket && websocketClient` from within the deepest hook itself instead of at configuration or call time. Since we can capture the configuration value from within the closure of the hook function, there is no need for additional checks at call site.
I.e.
```ts
// this is wrong
const wsHooks = enableWebsocket && websocketClient ? configureFooHooks() : undefined;
useFoo() {
wsHooks?.useFooUpdates() // <-- ! rules of hooks
}
configureFooHooks() {
return {
useFooUpdates: () => { ... implementation }
}
}
// this is verbose
const { useFooUpdates } = configureFooHooks()
useFoo() {
useFooUpdates()
}
configureFooHooks(client) {
if (!client) return {
useFooUpdates: () => { /* no-op hook */ }
} else return {
useFooUpdates: () => { ... implementation }
}
}
// this is correct
const { useFooUpdates } = configureFooHooks()
useFoo() {
useFooUpdates()
}
configureFooHooks(client) {
return {
useFooUpdates() { // <-- single definition
useEffect() {
if (!client || !wsEnabled) return; // <-- call stack is preserved so no rules of hooks violation
else // ... implementation
}
...
}
}
}
```
_Originally posted by @dialexo in https://github.com/graasp/graasp-apps-query-client/pull/142#discussion_r1297403293_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.