Skip to content

Commit db81a82

Browse files
committed
correct docs about edge cases and gotchas
1 parent 3ff6fa7 commit db81a82

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

docs/edge-cases.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Edge cases
22

3-
Structura supports some edge cases that are not always well handled by other libraries. On the contrary, some edge cases are not well supported.
3+
Structura supports some edge cases that are not always well handled by other libraries.
44

55
## Returning and modifying in the same producer
66

@@ -68,21 +68,35 @@ const newState5 = produce(state, (draft) => {
6868
})
6969
```
7070

71-
## Assigning draftable objects to non draftable objects
71+
## Transpositions
7272

73-
If you try assigning a draftable object to a non draftable object as a sub property, you will be left with a dangling proxy
73+
You can reassign the key of a sub-object and the result will be what you expect
74+
75+
However, this will not work if you assign to an object external from the draft
7476

7577
```typescript
7678
const state = [[1], [2]]
7779

78-
// this does not work well because the new object
79-
// will remain with a proxy attached via its prop;
80+
// this works well
81+
const newState1 = produce(state, (draft) => {
82+
const first = draft[0]
83+
draft[0] = draft[1]
84+
draft[1] = first
85+
})
8086

87+
// this also works
88+
const newState2 = produce(state, (draft) => {
89+
const first = draft[0]
90+
draft[0] = draft[1]
91+
draft[1] = first
92+
first.push(9999)
93+
})
94+
95+
// this does not work well because the new object
96+
// will remain with a proxy attached via its prop
8197
const newState3 = produce(state, (draft) => {
8298
const first = draft[0]
8399
const newObj = { prop: first }
84100
draft.push(newObj as any)
85101
})
86-
87-
// newState3[2].prop is a proxy
88102
```

docs/when-to-use.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
- test coverage is still not 100%
1616
- docs could be better
1717
- does not support IE and pre ES6 browsers and will never do
18-
- does not yet support ~~some proxy traps and~~ an edge case that Immer supports ([this](./edge-cases.md#assigning-draftable-objects-to-non-draftable-objects)), but this is very likely to change in the future
18+
- does not yet support ~~some proxy traps and~~ an edge case that Immer supports ([this](./gotchas.md#potential-dangling-proxy-references-if-you-assign-unproxied-objects-into-the-draft)), but this is very likely to change in the future
1919
- generated patches are not compliant to any RFC, but ~~in the future this may change or there could be a converter~~ there is already a converter included in the library; besides applyPatches also accept standard JSON Patches

0 commit comments

Comments
 (0)