Replies: 4 comments 23 replies
-
|
Our solution is |
Beta Was this translation helpful? Give feedback.
-
|
Hello. In our case, shallowProxy would be very useful. We use objects where one field should be tracked, and the rest of the fields should not. And new fields are periodically added. We have to constantly make sure that all new fields are wrapped in a ref. It is inconvenient, and it could be easy forgotten. |
Beta Was this translation helpful? Give feedback.
-
|
@overthemike Any thougths? |
Beta Was this translation helpful? Give feedback.
-
|
I don't want to to hijack, it seems it's more about Vue's shallowReactive in general and not specifically shallowRef. I stumbled upon this topic while trying to do the thing that the title suggests and also implement Vue unwrapping . I don't think doing this automagically was a good move any way because it causes too much surprises, explicit unwrapping is easier to implement including TS. Btw Valtio's I'm trying to make proxies work with react-like refs, which seems reasonable for the integration. I've tried to hook into import { type RefObject, useRef } from 'react';
import { proxy, ref as raw, unstable_replaceInternalFunction } from 'valtio';
const UNWRAP = Symbol.for('unwrap');
function unwrap<T>(value: RefObject<T>): T {
return Object.assign(value, { [UNWRAP]: true }) as T;
}
unstable_replaceInternalFunction('canProxy', (prevFn) => {
return (value: any) => {
if (value?.[UNWRAP]) {
return false;
}
return prevFn(value);
};
});
unstable_replaceInternalFunction('createHandler', (prevFn) => {
return (...args) => {
const handler = prevFn(...args);
handler.get = (target, prop, receiver) => {
const value = Reflect.get(target, prop, receiver)
return value?.[UNWRAP] ? value.current : value;
}
return handler;
};
});const wrappedRawRef = useRef({ a: { b: 1 } });
const rawRef = useRef({ a: { b: 1 } });
const shallowRef = useRef(raw(({ a: { b: 1 } })));
const deepRef = useRef({ a: { b: 1 } });
const state = useRef(proxy(
{
wrappedRawRef: raw(wrappedRawRef),
rawRef: unwrap(raw(rawRef)),
shallowRef: unwrap(shallowRef),
deepRef: unwrap(proxy(deepRef))
})).current;It appears to work as expected so far: @dai-shi What do you think about this approach? It seems that get trap is a good place to do this I'm hesitating about unstable_replaceInternalFunction, and it doesn't look right to me to modify a global for local purpose. Are there plans to make these hooks public, could proxy() and other core api accept options for customization? I've seen valtio-plugin. It seems to be generally aimed at higher level ops, what's the status of it? |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
It would be nice to be able to create a shallow proxy, something that only does a reactive transform to the first level. Vue's "shallowRef" (https://vuejs.org/api/reactivity-advanced.html#shallowref) has this behavior - and it makes it easier to "ingest" large/weird objects. Sometimes I only care about dependency tracking at the top level, because the rest of it is tracked/managed elsewhere. I think this would give a good path forward in cases similar to #782.
Beta Was this translation helpful? Give feedback.
All reactions