Modify JSON inserted/retrived from shared type

Yjs newbie here! :wave:

I’m trying to understand the documentation to learn how I can modify JSON inserted in the shared type. According to the Caveats section on working with shared types, it is discouraged to modify JSON inserted/retrieved from shared type. The example listed there directly modifies JSON as follows.

// 2. It is discouraged to modify JSON that is inserted or retrieved from a Yjs type
//    This might lead to documents that don't synchronize anymore.
const myObject = { val: 0 }
ymap.set(0, myObject)
ymap.get(0).val = 1 // Doesn't throw an error, but is highly discouraged
myobject.val = 2 // Also doesn't throw an error, but is also discouraged.

The document doesn’t tell what is the correct way to modify the JSON. I’m wondering if the following is the recommended way (seems to work)?

const myObject = { val: 0 }
ymap.set(0, myObject)
let json = ymap.get(0)
json.val = 2
ymap.set(0, json)
1 Like