electric-sql / electric-sql/pglite

`useLiveQuery` needs a default value.

Open
#544 4 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

Currently, useLiveQuery returns `undefined` until the initial query has returned. This leads to a bunch of `if results !=== undefined` checks in your component code.

For example:

```ts
function Component() {
const results = useLiveQuery(`
SELECT * FROM items
WHERE foo = $1
`, ['bar'])
const items = results !== undefined ? results.rows : []
// ...
```

Now, if you look at `useState`, it avoids this by supporting a default value:

```ts
function Component() {
const [results, setResults] = useState([])
// ...
```

I propose the ergonomic improvement of supporting a default value with `useLiveQuery`:

```ts
function Component() {
const { rows: items } = useLiveQuery(`
SELECT * FROM items
WHERE foo = $1
`, ['bar'], [])
// ...
```

Using a third positional argument for it is slightly tricky with query parameters (hence cherry picking an example of a query that uses parameters), because then you need to specify the query parameters arg even if not using them. I guess an alternative would be a `{default: value}` option (more explicit but also more verbose).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.