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(reactivity): trigger the ref that is created by toRef from a reactive array #12431

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ComputedRef, computed } from '../src/computed'
import { isReactive, reactive, shallowReactive, toRaw } from '../src/reactive'
import { isRef, ref } from '../src/ref'
import { isRef, ref, toRef, triggerRef } from '../src/ref'
import { effect } from '../src/effect'

describe('reactivity/reactive/Array', () => {
Expand Down Expand Up @@ -279,6 +279,17 @@ describe('reactivity/reactive/Array', () => {
expect(proxy).toHaveLength(1)
})

// #12427
test('trigger the ref that is created by toRef from a reactive Array', () => {
const array = reactive<number[]>([1])
const first = toRef(array, 0)
const fn = vi.fn()
effect(() => fn(first.value))
expect(fn).toHaveBeenCalledTimes(1)
triggerRef(first)
expect(fn).toHaveBeenCalledTimes(2)
})

describe('Array methods w/ refs', () => {
let original: any[]
beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,7 @@ export function getDepFromReactive(
key: string | number | symbol,
): Dep | undefined {
const depMap = targetMap.get(object)
// #12427, while the target object is an array, the dep key(array index) must be string
noootwo marked this conversation as resolved.
Show resolved Hide resolved
if (isArray(object) && typeof key === 'number') key = key.toString()
return depMap && depMap.get(key)
}