Skip to content

Commit 8f8db67

Browse files
authored
fix(reactive): fix unexpect effect in reactions (#2563)
1 parent 35a18c4 commit 8f8db67

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/core/src/__tests__/field.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createForm } from '../'
2+
import { DataField } from '../types'
23
import { attach, sleep } from './shared'
34

45
test('create field', () => {
@@ -1810,3 +1811,26 @@ test('custom validator to get ctx.field', async () => {
18101811
expect(!!ctxField).toBeTruthy()
18111812
expect(!!ctxForm).toBeTruthy()
18121813
})
1814+
1815+
test('single direction linkage effect', async () => {
1816+
const form = attach(createForm())
1817+
1818+
const input1 = form.createField({
1819+
name: 'input1',
1820+
reactions: (field: DataField) => {
1821+
if (!field.selfModified) {
1822+
return
1823+
}
1824+
input2.value = field.value
1825+
},
1826+
})
1827+
1828+
const input2 = form.createField({
1829+
name: 'input2',
1830+
})
1831+
1832+
await input1.onInput('123')
1833+
expect(input2.value).toBe('123')
1834+
await input2.onInput('321')
1835+
expect(input2.value).toBe('321')
1836+
})

packages/reactive/src/autorun.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
releaseBindingReactions,
66
disposeEffects,
77
hasDepsChange,
8+
untrackStart,
9+
untrackEnd,
810
} from './reaction'
911
import { isFn } from './checkers'
1012
import { ReactionStack } from './environment'
@@ -117,10 +119,12 @@ export const reaction = <T>(
117119

118120
const fireAction = () => {
119121
try {
122+
untrackStart()
120123
batchStart()
121124
if (isFn(subscriber)) subscriber(value.currentValue, value.oldValue)
122125
} finally {
123126
batchEnd()
127+
untrackEnd()
124128
}
125129
}
126130

0 commit comments

Comments
 (0)