Skip to content

Convex React client requires "react-dom" to be installed even on react-native only projects #74

Closed
@saviocmc

Description

@saviocmc

In the docs here, convex instructs to use the default Convex React client library for React Native projects.

When trying to do so, this code here

if (typeof ReactDOM === "undefined") {
throw new Error("Required dependency 'react-dom' not found");
}
throw an error when react-dom is not installed in the project. This should not be the case, because it is a valid use case to use convex in a React Native only project (with no react-dom or react-native-web).

The only use I could spot for the react-dom package in the code was to call unstable_batchedUpdates in the transition method:

private transition(updatedQueries: QueryToken[]) {
ReactDOM.unstable_batchedUpdates(() => {
for (const queryToken of updatedQueries) {
const callbacks = this.listeners.get(queryToken);
if (callbacks) {
for (const callback of callbacks) {
callback();
}
}
}
});
}
}

Looking it up I saw that it represented a performance gain in react 17 and prior versions, but since react 18 (2022) this is no more the case as it introduced Automatic Batching, as explained by Dan Abramov in this discussion: reactwg/react-18#21.

Also the unstable_batchedUpdates is an undocumented API that could probably be removed in future releases.

I see that convex aims to support react 17 (as it lists it as a valid peer dependence in the package.json). If dropping support for react 17 is not in the plans, the convex client should use the react-native implementation of unstable_batchedUpdates when running on a native platform.

So, I propose that convex could check if the react version is 17 and then try to import unstable_batchedUpdates from react-dom if running on the browser or import it from react-native when running on a native platform.

I also think this API should not be used in case react version is 18 or greater than 18, as it make no diference with the Automatic Batching implementation. In the future, if the support for react 17 is dropped, it makes it easier to remove only the code that tests if running on react 17.

Currently I use convex in my "React Native only" app (with react 18 and no react-dom). I just patched the client removing the line that throws the error when react-dom is not found and it works as expected.

What do you think? Should I submit a PR with this proposed changes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions