File tree 2 files changed +10
-4
lines changed
2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,10 @@ describe('reactivity/ref', () => {
236
236
// mutating source should trigger effect using the proxy refs
237
237
a . x = 4
238
238
expect ( dummyX ) . toBe ( 4 )
239
+
240
+ // should keep ref
241
+ const r = { x : ref ( 1 ) }
242
+ expect ( toRef ( r , 'x' ) ) . toBe ( r . x )
239
243
} )
240
244
241
245
test ( 'toRefs' , ( ) => {
@@ -292,12 +296,12 @@ describe('reactivity/ref', () => {
292
296
test ( 'toRefs reactive array' , ( ) => {
293
297
const arr = reactive ( [ 'a' , 'b' , 'c' ] )
294
298
const refs = toRefs ( arr )
295
-
299
+
296
300
expect ( Array . isArray ( refs ) ) . toBe ( true )
297
-
301
+
298
302
refs [ 0 ] . value = '1'
299
303
expect ( arr [ 0 ] ) . toBe ( '1' )
300
-
304
+
301
305
arr [ 1 ] = '2'
302
306
expect ( refs [ 1 ] . value ) . toBe ( '2' )
303
307
} )
Original file line number Diff line number Diff line change @@ -168,7 +168,9 @@ export function toRef<T extends object, K extends keyof T>(
168
168
object : T ,
169
169
key : K
170
170
) : Ref < T [ K ] > {
171
- return new ObjectRefImpl ( object , key ) as any
171
+ return isRef ( object [ key ] )
172
+ ? object [ key ]
173
+ : ( new ObjectRefImpl ( object , key ) as any )
172
174
}
173
175
174
176
// corner case when use narrows type
You can’t perform that action at this time.
0 commit comments