CRUD - Update - React Native SDK
The examples on this page use the following schema:
Update an Object
You can add, modify, or delete properties of a Realm object in the same way that you would update any other JavaScript object. But, you must do it inside of a write transaction.
In the following example of a TaskItem
component, we:
Get access to the opened realm instance by calling the
useRealm()
hook within the component.Retrieve a task by calling
useObject()
with "Task" and the_id
parameter of the component.Create a component method
incrementTaskProgress()
that performs a write transaction and increments the task'sprogressMinutes
.Render the task's
name
andprogressMinutes
in the UI.Add an onPress event on the "increment" button that calls
incrementTaskProgress()
.
Tip
Update Related and Embedded Objects
To update a property of an embedded object or a related object, modify the property with dot-notation or bracket-notation as if it were in a regular, nested object.
Upsert an Object
To upsert an object within a write transaction, call Realm.create() with the update mode set to modified
. The operation
either inserts a new object with the given primary key or updates an existing
object that already has that primary key.
Note
Upserting Requires Realm.create()
You must call Realm.create()
within a write transaction to upsert an object.
This is different than creating a new Realm Objects by
calling the new operator.
In the following example of a CreateTaskItem
component we:
Get access to the opened realm instance by calling the
useRealm()
hook within the component.Perform a write transaction, and create a
Task
object with an_id
value of1234
.Call
Realm.create()
inside the write transaction to upsert aTask
object by specifying the same_id
and a different``progressMinutes`` and the update mode set to "modified".Render the task's
name
andprogressMinutes
in the UI, showing the modified progress.
Bulk Update a Collection
To apply an update to a collection of objects, iterate through the collection (e.g. with for...of). In the loop, update each object individually.
In the following example of a TaskDashboard
component, we:
Get access to the opened realm instance by calling the
useRealm()
hook within the component.Retrieve all tasks in the realm instance by passing
Task
to theuseQuery()
hook.Create a component method
resetProgressOnAllTasks()
that performs a write transaction. Within that write transaction, we bulk update all tasks by looping through them usingfor...of
and set theirprogressMinutes
to 0.Map through the tasks to render a list of
Text
components displaying each task'sname
andprogressMinutes
.
const TaskDashboard = () => { const realm = useRealm(); const tasks = useQuery(Task); const resetProgressOnAllTasks = () => { realm.write(() => { for (const task of tasks) { task.progressMinutes = 0; } }); }; return ( <> {tasks.map(task => { <Text> {task.name} has {task.progressMinutes} minutes progressed </Text>; })} <Button onPress={resetProgressOnAllTasks} title='Reset Progress' /> </> ); };