-
Notifications
You must be signed in to change notification settings - Fork 88
(DOCSP-15010): Using Change Listeners In Your Components - React Native SDK #996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mohammadhunan-dev
merged 38 commits into
mongodb:master
from
mohammadhunan-dev:DOCSP-15010-mutable-objects-react-native
Apr 29, 2021
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d3b0c29
(DOCSP-15010): Reacting to changes from components
e37787f
removed unneeded text
2b94412
update
fdd9167
add instructions to class components
e8acbd3
separated react content from react-to-changes
c442b18
added rn files to its own examples folder under node + excluded them …
eca304b
fix file names
f7f7745
added generated files
f116dc0
Add emphasize lines
968bac4
fix conflicts
c37ac63
re-add class component
6d0b62a
re-add functional component
f877c51
fix toc tree
c47c681
update wording
2e78747
fix grammar
88ec793
updated title to avoid gerunds
ade339d
fix typo our->your
a2b780d
replace {+realm+} objects with {+service-short+} objects
72cfe5a
fix wording
a2a70bc
update
c50fbaf
removed redudant class component example
4e17453
Update source/sdk/react-native/examples/react-to-changes.txt
9d67128
fix spacing
616781b
fixed Reactjs -> React.js
c474a46
update wording
32000ae
typo fix
bcd40ae
update wording
64a99dc
switched component name
af8bfcf
add example directive
2ee9860
fix example paragraph
0e20a70
fix wording and grammar
33fdb0b
fix grammar
8e74475
fix typo
1e42590
Update source/sdk/react-native/examples/use-change-listeners-in-compo…
66c19bf
Update source/sdk/react-native/examples/use-change-listeners-in-compo…
a5c2006
Update source/sdk/react-native/examples/use-change-listeners-in-compo…
81e103d
added emphasize to non-generated file
da929b3
switched literalinclude to include for rst formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
examples/node/Examples/rn/using-change-listeners-functional-component.js
This file contains hidden or 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,57 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import {Text} from 'react-native'; | ||
import Realm from 'realm'; | ||
// :code-block-start: using-change-listeners-functional-component | ||
const TaskList = () => { | ||
const [tasks, setTasks] = useState([]); | ||
// :emphasize-start: | ||
useEffect(() => { | ||
// :emphasize-end: | ||
Realm.open({ | ||
schema: [TaskSchema], // predefined schema | ||
}).then(realm => { | ||
|
||
const tasks = realm.objects('Task'); | ||
// set state to the initial value of your realm objects | ||
setTasks([...tasks]); | ||
|
||
// :emphasize-start: | ||
tasks.addListener(() => { | ||
// update state of tasks to the updated value | ||
setTasks([...tasks]); | ||
}); | ||
// :emphasize-end: | ||
|
||
realm.write(() => { | ||
// the following tasks will trigger the change listener and update the UI | ||
realm.create('Task', { | ||
name: 'Go to the grocery store', | ||
}); | ||
realm.create('Task', { | ||
name: 'Exercise in the gym', | ||
}); | ||
}); | ||
|
||
// cleanup function | ||
// :emphasize-start: | ||
return () => { | ||
const tasks = realm.objects('Task'); | ||
// Remember to remove the listener when you're done! | ||
tasks.removeAllListeners(); | ||
// :emphasize-end: | ||
// Call the close() method when done with a realm instance to avoid memory leaks. | ||
realm.close(); | ||
}; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{tasks.map(task => ( | ||
<Text key={task.name}>{task.name}</Text> | ||
))} | ||
</> | ||
); | ||
}; | ||
// :code-block-end: | ||
export default TaskList; |
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
...mponent.codeblock.using-change-listeners-functional-component.js.code-block.rst
This file contains hidden or 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,48 @@ | ||
.. code-block:: javascript | ||
:emphasize-lines: 3, 12-15, 28-31 | ||
|
||
const TaskList = () => { | ||
const [tasks, setTasks] = useState([]); | ||
useEffect(() => { | ||
Realm.open({ | ||
schema: [TaskSchema], // predefined schema | ||
}).then(realm => { | ||
|
||
const tasks = realm.objects('Task'); | ||
// set state to the initial value of your realm objects | ||
setTasks([...tasks]); | ||
|
||
tasks.addListener(() => { | ||
// update state of tasks to the updated value | ||
setTasks([...tasks]); | ||
}); | ||
|
||
realm.write(() => { | ||
// the following tasks will trigger the change listener and update the UI | ||
realm.create('Task', { | ||
name: 'Go to the grocery store', | ||
}); | ||
realm.create('Task', { | ||
name: 'Exercise in the gym', | ||
}); | ||
}); | ||
|
||
// cleanup function | ||
return () => { | ||
const tasks = realm.objects('Task'); | ||
// Remember to remove the listener when you're done! | ||
tasks.removeAllListeners(); | ||
// Call the close() method when done with a realm instance to avoid memory leaks. | ||
realm.close(); | ||
}; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{tasks.map(task => ( | ||
<Text key={task.name}>{task.name}</Text> | ||
))} | ||
</> | ||
); | ||
}; |
This file contains hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
source/sdk/react-native/examples/use-change-listeners-in-components.txt
This file contains hidden or 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,63 @@ | ||
.. _react-native-use-listeners-in-components: | ||
|
||
===================================================== | ||
Use Change Listeners In Components - React Native SDK | ||
===================================================== | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
|
||
Overview | ||
-------- | ||
You can copy your {+service-short+} objects to your component's :reactjs:`state | ||
<docs/state-and-lifecycle.html>`. However, since {+service-short+} objects are | ||
live and automatically update in response to changes, you must update the copies | ||
of them to prevent your UI from drifting out of date with underlying data. You can do this by | ||
registering a :ref:`change listener <react-native-change-notifications>` in your | ||
component and updating the state variable when that listener fires. | ||
|
||
.. warning:: | ||
|
||
Failing to update copies of {+service-short+} objects leads to out-of-date data displayed | ||
in your UI as objects are deleted or changed. | ||
|
||
Procedure | ||
~~~~~~~~~ | ||
To keep your UI up-to-date with changes to underlying {+service-short+} objects, | ||
declare a state variable for your {+service-short+} objects using the | ||
:reactjs:`useState() <docs/hooks-state.html>` hook. | ||
|
||
Within the :reactjs:`useEffect() <docs/hooks-effect.html>` hook, set the state variable to | ||
the initial value of your objects. Then declare a change listener on the | ||
{+service-short+} objects. | ||
|
||
Finally, return an anonymous cleanup function that you can use to remove the | ||
change listener and close the {+service-short+}. React.js will call this cleanup function when | ||
the component unmounts. | ||
|
||
.. example:: | ||
|
||
In the following example, a developer creates an application to manage | ||
tasks. Within a ``TaskList`` component, define a state variable called | ||
``tasks``. The developer wants to display the initial list of tasks already | ||
saved in the database after the component mounts. To do this, they :ref:`open | ||
a realm <react-native-open-a-local-realm>` within a ``useEffect`` function and | ||
then set the ``tasks`` state variable to the initial value of the ``tasks`` | ||
stored in the {+service-short+}. | ||
|
||
The developer registers a change listener to update the state variable when | ||
changes to the ``tasks`` have been made in the {+client-database+}. The | ||
developer then creates some additional tasks. | ||
|
||
Once the component unmounts, the developer wants to unregister the change | ||
listener. To do this, they return a cleanup function and call ``task.removeAllListeners()``. | ||
Finally they :ref:`close the realm <react-native-close-a-realm>`. | ||
|
||
.. include:: /examples/generated/rn/using-change-listeners-functional-component.codeblock.using-change-listeners-functional-component.js.code-block.rst | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly to above, I think we're missing the why of this paragraph. I understand that we need to clean up the listener when we're done displaying the object on the UI and no longer need the listener... but that might not be obvious to a reader who's just learning about realm. Also, should "Reactjs" be "ReactJS" or "React.js" or even just "React"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.