Skip to content

Commit

Permalink
fix(shallowReactive): should keep array as array (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Jun 4, 2021
1 parent f08a1d6 commit 620d09b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/reactivity/reactive.ts
Expand Up @@ -156,7 +156,7 @@ export function shallowReactive(obj: any): any {
return obj as any
}

const observed = observe({})
const observed = observe(isArray(obj) ? [] : {})
setupAccessControl(observed)

const ob = (observed as any).__ob__
Expand Down
18 changes: 18 additions & 0 deletions test/v3/reactivity/reactive.spec.ts
Expand Up @@ -206,6 +206,24 @@ describe('reactivity/reactive', () => {
props.n = reactive({ foo: 2 })
expect(isReactive(props.n)).toBe(true)
})

test('should keep array as array', () => {
const arr = [1, 2, 3]
const shallowReactiveArr = shallowReactive(arr)
expect(Array.isArray(shallowReactiveArr)).toBe(true)
expect(shallowReactiveArr.join(' ')).toBe(arr.join(' '))
})

test('should trigger computed when changed', () => {
const arr = Array(10).fill(0)
const shallowReactiveArr = shallowReactive(arr)
const sum = computed(() =>
shallowReactiveArr.reduce((acc, cur) => acc + cur, 0)
)
expect(sum.value).toBe(0)
shallowReactiveArr[0] = 1
expect(sum.value).toBe(1)
})
})

test('should shallowReactive non-observable values', () => {
Expand Down

0 comments on commit 620d09b

Please sign in to comment.