Skip to content

Commit 4f0f1b9

Browse files
committed
Update example descriptions
1 parent 03fc17a commit 4f0f1b9

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

source/examples/generated/react-native/ts/embedded-objects-test.snippet.update-embedded-object.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Find the contact you want to update
22
const UpdateContact = ({contactName}: {contactName: string}) => {
3-
const contact = useQuery(Contact).filtered(`name == '${contactName}'`)[0];
43
const [street, setStreet] = useState('');
4+
const contact = useQuery(Contact).filtered(`name == '${contactName}'`)[0];
55
const realm = useRealm();
66

77
const updateStreet = () => {

source/sdk/react-native/realm-database/schemas/embedded-objects.txt

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,25 @@ Create an Embedded Object
104104
~~~~~~~~~~~~~~~~~~~~~~~~~
105105

106106
To create an embedded object, assign an instance of the embedded object
107-
to a parent object's property:
107+
to a parent object's property.
108+
109+
Example
110+
```````
111+
112+
In the following ``CreateContact`` example, we create a new ``Contact`` object
113+
with an embedded ``Address`` object.
114+
115+
The ``CreateContact`` component does the following:
116+
117+
- Creates React `state <https://reactjs.org/docs/react-component.html#state>`__ variables that
118+
represent the contact's name and address details.
119+
- Gets access to an open realm instance by calling the ``useRealm()`` hook within the
120+
component.
121+
- Creates a component method ``submitContact()`` that performs a write transaction
122+
to create a new ``Address`` embedded object and a new ``Contact`` parent object based
123+
on the ``TextInput`` values for the contact's name and address details.
124+
- Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
125+
submit button that calls ``submitContact()``
108126

109127
.. tabs-realm-languages::
110128

@@ -127,9 +145,22 @@ to a parent object's property:
127145
Query a Collection on Embedded Object Properties
128146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129147

130-
Use dot notation to filter or sort a :ref:`collection
148+
You can use dot notation to filter or sort a :ref:`collection
131149
<react-native-results-collections>` of objects based on an embedded object
132-
property value:
150+
property value.
151+
152+
Example
153+
```````
154+
155+
In the following ``ContactList`` example, we filter and query an embedded
156+
``Address`` object.
157+
158+
The ``ContactList`` component does the following:
159+
160+
- Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook.
161+
- Filters for contacts with the name ``John Smith`` by passing :js-sdk:`collection.filtered()
162+
<Realm.Collection.html#filtered>` on the query: ``"name == 'John Smith'"``.
163+
- Retrieves the property value of the contact's street address by using dot notation.
133164

134165
.. tabs-realm-languages::
135166

@@ -153,7 +184,27 @@ Update an Embedded Object Property
153184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154185

155186
To update a property in an embedded object, modify the property in a
156-
write transaction:
187+
write transaction.
188+
189+
Example
190+
```````
191+
192+
In the following ``UpdateContact`` example, we update the ``street`` property for
193+
an embedded ``Address`` object.
194+
195+
The ``UpdateContact`` component does the following:
196+
197+
- Creates a React `state <https://reactjs.org/docs/react-component.html#state>`__ variable
198+
that represents the contact's new street address.
199+
- Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook
200+
and filters for the contact that matches the name passed into the component as a prop.
201+
- Gets access to an opened realm instance by calling the ``useRealm()`` hook within the
202+
component.
203+
- Creates a component method ``updateStreet()`` that performs a write transaction and uses
204+
dot notation to set the contact's street address to the value of the ``street`` state variable.
205+
- Renders a ``TextInput`` that displays and changes the ``street`` state variable.
206+
- Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
207+
``"Update Street Address"`` button that calls ``updateStreet()``.
157208

158209
.. tabs-realm-languages::
159210

@@ -177,7 +228,26 @@ Overwrite an Embedded Object
177228
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178229

179230
To overwrite an embedded object, reassign the embedded object property
180-
of a party to a new instance in a write transaction:
231+
of a party to a new instance in a write transaction.
232+
233+
Example
234+
```````
235+
236+
In the following ``OverwriteContact`` example, we overwrite an embedded ``Address`` object.
237+
238+
The ``OverwriteContact`` component does the following:
239+
240+
- Creates React `state <https://reactjs.org/docs/react-component.html#state>`__ variables
241+
that represent the contact's new address details.
242+
- Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook
243+
and filters for the contact that matches the name passed into the component as a prop.
244+
- Gets access to an opened realm instance by calling the ``useRealm()`` hook within the
245+
component.
246+
- Creates a component method ``updateAddress()`` that performs a write transaction to
247+
create a new ``Address`` object based on the ``TextInput`` values for the
248+
contact's address details and update the ``Contact`` object with the new address.
249+
- Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
250+
``"Overwrite Address"`` button that calls ``updateAddress()``.
181251

182252
.. tabs-realm-languages::
183253

@@ -202,6 +272,22 @@ Delete an Embedded Object
202272
Realm Uses Cascading Deletes for Embedded Objects. To delete an embedded object,
203273
delete the embedded object's parent.
204274

275+
Example
276+
```````
277+
278+
In the following ``DeleteContact`` example, we delete an embedded object and its
279+
parent object.
280+
281+
The ``DeleteContact`` component does the following:
282+
283+
- Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook.
284+
- Gets access to an open realm instance by calling the ``useRealm()`` hook within the component.
285+
- Creates a component method ``deleteContact()`` that performs a write transaction and calls
286+
:js-sdk:`Realm.delete() <Realm.html#delete>` to remove the ``Contact`` object that matches
287+
the name passed into the component as a prop.
288+
- Add an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the "Delete Contact``
289+
button that calls ``deleteContact()``.
290+
205291
.. tabs-realm-languages::
206292

207293
.. tab::

0 commit comments

Comments
 (0)