Valtio/V2/Deep Clone Proxy Objects

/icons/calendar.svg

Last update

Aug 30, 2024

In Valtio v2, the behavior of the proxy(obj) function has changed. In v1, proxy(obj) was a pure function that deeply copied the object obj. In v2, proxy(obj) is an impure function that deeply modifies obj. This codemod updates your code to accommodate these changes by incorporating deepClone to ensure that objects are deeply copied as they were in v1.

Example

Before

import { proxy } from 'valtio';
const state = proxy({ count: 1, obj: { text: 'hi' } });
state.obj = { text: 'hello' };

After

import { proxy } from 'valtio';
import { deepClone } from 'valtio/utils';
const state = proxy(deepClone({ count: 1, obj: { text: 'hi' } }));
state.obj = deepClone({ text: 'hello' });

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now