Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shallowReactive): should keep array as array #717

Merged
merged 1 commit into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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