Skip to content

Commit e6224f4

Browse files
bcq028antfu
andauthoredOct 21, 2022
fix(reactivity): enable trigger when use str to set length of arr (#6810)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent e5fc7dc commit e6224f4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
 

‎packages/reactivity/__tests__/effect.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,22 @@ describe('reactivity/effect', () => {
922922
expect(fnSpy2).toHaveBeenCalledTimes(1)
923923
})
924924

925+
it('should be triggered when set length with string', () => {
926+
let ret1 = 'idle'
927+
let ret2 = 'idle'
928+
const arr1 = reactive(new Array(11).fill(0))
929+
const arr2 = reactive(new Array(11).fill(0))
930+
effect(() => {
931+
ret1 = arr1[10] === undefined ? 'arr[10] is set to empty' : 'idle'
932+
})
933+
effect(() => {
934+
ret2 = arr2[10] === undefined ? 'arr[10] is set to empty' : 'idle'
935+
})
936+
arr1.length = 2
937+
arr2.length = '2' as any
938+
expect(ret1).toBe(ret2)
939+
})
940+
925941
describe('readonly + reactive for Map', () => {
926942
test('should work with readonly(reactive(Map))', () => {
927943
const m = reactive(new Map())

‎packages/reactivity/src/effect.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TrackOpTypes, TriggerOpTypes } from './operations'
2-
import { extend, isArray, isIntegerKey, isMap } from '@vue/shared'
2+
import { extend, isArray, isIntegerKey, isMap, toNumber } from '@vue/shared'
33
import { EffectScope, recordEffectScope } from './effectScope'
44
import {
55
createDep,
@@ -277,7 +277,7 @@ export function trigger(
277277
deps = [...depsMap.values()]
278278
} else if (key === 'length' && isArray(target)) {
279279
depsMap.forEach((dep, key) => {
280-
if (key === 'length' || key >= (newValue as number)) {
280+
if (key === 'length' || key >= toNumber(newValue)) {
281281
deps.push(dep)
282282
}
283283
})

0 commit comments

Comments
 (0)
Please sign in to comment.