Skip to content

Commit

Permalink
useUser fixes for Realm React
Browse files Browse the repository at this point in the history
* Used a different type declaration generation method which solved typing issues
* Updated `UserProvider` to always update the user reference on user events
* Updated connection example to include a delete user function to test this feature
  • Loading branch information
takameyer committed Oct 20, 2023
1 parent eca95b6 commit 039cd2d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const styles = StyleSheet.create({
borderRadius: 30,
borderColor: colors.purple,
backgroundColor: colors.purple,
justifyContent: 'center',
},
buttonSecondary: {
paddingVertical: 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import React from 'react';
import {Alert, FlatList, StyleSheet, Text, View} from 'react-native';
import {useAuth} from '@realm/react';
import {useAuth, useApp, useUser} from '@realm/react';

import {Button} from '../components/Button';
import {KioskItem} from '../components/KioskItem';
Expand All @@ -43,6 +43,9 @@ export function StoreScreen() {
refreshAccessToken,
} = useDemoSyncTriggers();
const {logOut} = useAuth();
const user = useUser();
const app = useApp();


return (
<View style={styles.container}>
Expand Down Expand Up @@ -110,6 +113,14 @@ export function StoreScreen() {
onPress={refreshAccessToken}
text="Refresh Access Token"
/>
<Button
extraStyles={[styles.button]}
onPress={() => {
app.deleteUser(user);
app.removeUser(user);
}}
text="Delete User"
/>
</View>
</View>
</>
Expand Down
5 changes: 2 additions & 3 deletions packages/realm-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* None

### Enhancements
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* None
* Improved type generation, which fixed an issue with the `Realm.User` return from `useUser` ([#6196](https://github.com/realm/realm-js/issues/6196))
* `UserProvider` will now always return a new `user` reference on change events. ([#6186](https://github.com/realm/realm-js/issues/6186))

### Compatibility
* React Native >= v0.71.4
Expand Down
3 changes: 1 addition & 2 deletions packages/realm-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"wireit": {
"bundle": {
"command": "rollup --config",
"command": "rollup --config && tsc -p tsconfig.types.json",
"dependencies": [
"../realm:bundle",
"../realm-common:bundle"
Expand Down Expand Up @@ -71,7 +71,6 @@
"react-native": "0.72.6",
"react-test-renderer": "18.2.0",
"realm": "*",
"rollup-plugin-dts": "^5.0.0",
"ts-jest": "^29.0.5"
},
"peerDependencies": {
Expand Down
14 changes: 0 additions & 14 deletions packages/realm-react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import nodeResolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import dts from "rollup-plugin-dts";

import pkg from "./package.json" assert { type: "json" };

Expand All @@ -40,17 +39,4 @@ export default [
plugins: [nodeResolve(), typescript({ noEmitOnError: true })],
external: ["realm", "react", "@realm/common", "lodash"],
},
{
input: "src/index.tsx",
output: {
file: pkg.types,
format: "es",
},
plugins: [
dts({
respectExternal: true,
}),
],
external: ["realm", "react", "@realm/common", "lodash"],
},
];
7 changes: 4 additions & 3 deletions packages/realm-react/src/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export const UserProvider: React.FC<UserProviderProps> = ({ fallback: Fallback,

useEffect(() => {
const event = () => {
if (app.currentUser?.id != user?.id) {
setUser(app.currentUser);
}
console.log("UserProvider: user changed");
console.log("UserProvider: app.currentUser", app.currentUser);
console.log("UserProvider: app.currentUser.id", app.currentUser);
setUser(app.currentUser);
};
user?.addListener(event);
app?.addListener(event);
Expand Down
11 changes: 11 additions & 0 deletions packages/realm-react/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "./dist",
},
"exclude": [
"**/__tests__/*"
]
}

0 comments on commit 039cd2d

Please sign in to comment.