Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of connecting a Realm React app to sync (#4356)
* Add an example of connecting a Realm React app to sync * Generate JS app * Enable flexible sync * Refactor Templates favor const over function for components favor named exports over default exports apply sync configuration at the index.js separate sync and non-sync apps for readability remove the anon login flow move the sync setup steps into their own document create example app that is lerna aware create sync script to transmit example app source into all other templates update versions of realm/react and realm default template version to 0.0.1 favor type over interface wrap all in component function declarations in useCallback generate realm context in the index.ts of the models folder Co-authored-by: Andrew Meyer <andrew.meyer@mongodb.com>
- Loading branch information
Showing
183 changed files
with
6,229 additions
and
4,556 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ vendor/ | |
/integration-tests/ | ||
/install-tests/ | ||
/packages/ | ||
/examples/ | ||
/tests/ReactTestApp/ | ||
/templates/ | ||
/example/ |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: '@react-native-community', | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ build/ | |
.gradle | ||
local.properties | ||
*.iml | ||
*.hprof | ||
|
||
# node.js | ||
# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
bracketSpacing: false, | ||
jsxBracketSameLine: true, | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
arrowParens: 'avoid', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# React Native Template Realm TypeScript | ||
|
||
## Usage | ||
|
||
Simple React Native template to quickly get started with Realm. | ||
|
||
This app implements a simple todo list, using Realm for persistence and the [Realm React](https://github.com/realm/realm-js/tree/master/packages/realm-react) hooks for React integration. It supports sync, allowing users to login and sync their todo lists across multiple devices. | ||
|
||
## 🚀 How to use | ||
|
||
``` | ||
npx lerna bootstrap --scope=realm-example --include-dependencies | ||
``` | ||
|
||
## 🏃 How to build and run locally | ||
|
||
- [Setup React Native development Environment](https://reactnative.dev/docs/environment-setup) | ||
- Build/Run on iOS 🍎 | ||
``` | ||
npm run ios | ||
``` | ||
- Build/Run on Android 🤖 | ||
``` | ||
npm run android | ||
``` | ||
|
||
## 💻 Start the Dev Client | ||
|
||
``` | ||
npm start | ||
``` | ||
|
||
## 💾 Testing changes to the template when developing | ||
|
||
To test the template locally, run it like any other React Native app: `npm i && npx pod-install` then `npm run ios`. | ||
|
||
## 🔀 Setting up sync | ||
|
||
See https://github.com/realm/realm-js/blob/master/templates/docs/sync-setup.md for instructions. | ||
## 📝 Notes | ||
- [React Native docs](https://reactnative.dev/docs/getting-started) | ||
- [React Hooks](https://reactjs.org/docs/hooks-intro.html) | ||
- [Setting Up Realm Sync](https://docs.mongodb.com/realm/sdk/react-native/quick-start/) | ||
- [Realm JS Documentation](https://docs.mongodb.com/realm/sdk/react-native/) | ||
- [@realm/react Readme](https://github.com/realm/realm-js/tree/master/packages/realm-react#readme) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @format | ||
*/ | ||
|
||
import 'react-native'; | ||
import React from 'react'; | ||
import {AppWrapperNonSync} from '../app/AppWrapperNonSync'; | ||
|
||
// Note: test renderer must be required after react-native. | ||
import renderer from 'react-test-renderer'; | ||
|
||
it('renders correctly', () => { | ||
renderer.create(<AppWrapperNonSync />); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../android/app/src/main/AndroidManifest.xml → .../android/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">RealmTsTemplate</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...alm-react/testApp/android/settings.gradle → example/android/settings.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
rootProject.name = 'RealmReactExample' | ||
rootProject.name = 'RealmTsTemplate' | ||
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) | ||
include ':app' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "RealmTsTemplate", | ||
"displayName": "RealmTsTemplate" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, {useMemo} from 'react'; | ||
|
||
import {Task} from './models/Task'; | ||
import {TaskRealmContext} from './models'; | ||
import {TaskManager} from './components/TaskManager'; | ||
|
||
const {useQuery} = TaskRealmContext; | ||
|
||
export const AppNonSync = () => { | ||
const result = useQuery(Task); | ||
|
||
const tasks = useMemo(() => result.sorted('createdAt'), [result]); | ||
|
||
return <TaskManager tasks={tasks} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, {useEffect, useMemo} from 'react'; | ||
|
||
import {Task} from './models/Task'; | ||
import {TaskRealmContext} from './models'; | ||
import {TaskManager} from './components/TaskManager'; | ||
|
||
const {useRealm, useQuery} = TaskRealmContext; | ||
|
||
type AppProps = { | ||
userId: string; | ||
}; | ||
|
||
export const AppSync: React.FC<AppProps> = ({userId}) => { | ||
const realm = useRealm(); | ||
const result = useQuery(Task); | ||
|
||
const tasks = useMemo(() => result.sorted('createdAt'), [result]); | ||
|
||
useEffect(() => { | ||
realm.subscriptions.update(mutableSubs => { | ||
mutableSubs.add(result); | ||
}); | ||
}, [realm, result]); | ||
|
||
return <TaskManager tasks={tasks} userId={userId} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import {SafeAreaView, StyleSheet} from 'react-native'; | ||
|
||
import {TaskRealmContext} from './models'; | ||
import colors from './styles/colors'; | ||
import {AppNonSync} from './AppNonSync'; | ||
|
||
export const AppWrapperNonSync = () => { | ||
const {RealmProvider} = TaskRealmContext; | ||
|
||
// If sync is disabled, setup the app without any sync functionality and return early | ||
return ( | ||
<SafeAreaView style={styles.screen}> | ||
<RealmProvider> | ||
<AppNonSync /> | ||
</RealmProvider> | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
screen: { | ||
flex: 1, | ||
backgroundColor: colors.darkBlue, | ||
}, | ||
}); |
Oops, something went wrong.