8000 fix(renderList): handle readonly reactive arrays in renderList by linzhe141 · Pull Request #13090 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

fix(renderList): handle readonly reactive arrays in renderList #13090

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Fail 8000 ed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/runtime-core/__tests__/helpers/renderList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { isReactive, reactive, shallowReactive } from '../../src/index'
import {
isReactive,
reactive,
readonly,
ref,
shallowReactive,
} from '../../src/index'
import { renderList } from '../../src/helpers/renderList'

describe('renderList', () => {
Expand Down Expand Up @@ -65,4 +71,14 @@ describe('renderList', () => {
const shallowReactiveArray = shallowReactive([{ foo: 1 }])
expect(renderList(shallowReactiveArray, isReactive)).toEqual([false])
})

it('should warn when attempting to mutate a readonly reactive array', () => {
const reactiveArray = readonly(ref([{ x: 1 }])) as any
renderList(reactiveArray.value, m => {
m.x = 0
})
expect(
`Set operation on key "x" failed: target is readonly.`,
).toHaveBeenWarned()
})
})
4 changes: 3 additions & 1 deletion packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { VNode, VNodeChild } from '../vnode'
import {
isReactive,
isReadonly,
isShallow,
shallowReadArray,
toReactive,
Expand Down Expand Up @@ -67,7 +68,8 @@ export function renderList(
const sourceIsArray = isArray(source)

if (sourceIsArray || isString(source)) {
const sourceIsReactiveArray = sourceIsArray && isReactive(source)
const sourceIsReactiveArray =
sourceIsArray && !isReadonly(source) && isReactive(source)
let needsWrap = false
if (sourceIsReactiveArray) {
needsWrap = !isShallow(source)
Expand Down
0