Skip to content

React useRxDocument starts loading:false; hooks commit stale settled data on key/query change #8965

Description

@chris-rnwbl

Bug

1. useRxDocument first paint looks like "not found"

https://github.com/pubkey/rxdb/blob/master/src/plugins/react/hooks/use-rx-document.ts

const [result, setResult] = useState(null);
const [loading, setLoading] = useState(false); // should be true when a key is set

setLoading(true) runs only in useEffect. The first committed paint is loading: false and result: null. Callers that check loading before treating null as missing still get a false miss on that frame. Going from no key to a key has the same first paint.

#8292 / #8294 set initial loading: true for useRxQuery / useLiveRxQuery only. useRxDocument (#8272) still starts false.

2. Query or key change commits the previous result as settled

On query / primaryKey change, both useLiveRxQuery and useRxDocument keep the previous results / result with loading: false for one committed render. setLoading(true) in useEffect is too late.

Expected

  • useRxDocument: loading is true whenever a key is set and the current key has not emitted yet.
  • After a query or key change, the first committed paint must not present the previous result with loading: false.

Related

Is there a reason or convention behind this behavior? Many other react-hook libraries instead use a pretty aggressive loading: true state before things are resolved. I think Apollo's model is reasonably informative, especially with extended network statuses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions