Confusion re: v2 #938
Replies: 3 comments 3 replies
-
Thanks for pointing it out. It's actually confusing. Let me try to improve it. |
Beta Was this translation helpful? Give feedback.
-
For me this is still confusing. How I understand it, the problem does not happen when you only use this means it's a change in behavior that will for most users of valtio not cause any problems, if they only use the @dai-shi do you think this is the correct interpretation? import { proxy } from "valtio";
import { deepClone } from "valtio/utils";
const object = { count: 1, message: { text: "hi" } };
const state = proxy(object);
state.message.text = "hello"; // (1) this is fine.
state.message = { text: "hello" }; // (2) this is fine.
state.message = object.message; // (3) this is fine.
// (4) don't do this.
state.message = object.message;
object.message.text = "howdy";
// (5) this is fine.
state.message = deepClone(object.message);
object.message.text = "howdy"; it's still strange. when I modify // (5) this is fine.
state.message = deepClone(object.message);
object.message.text = "howdy"; so when you interact with const object = { count: 1, message: { text: "hi" } };
const state = proxy(deepClone(object)); |
Beta Was this translation helpful? Give feedback.
-
I took it to mean not to reuse objects you pass into the proxy function. For example, if you use composition to create multiple state objects and you reuse the same initial state object reference, it might bite you. Example: const initialState = {
id: null,
name: '',
description: ''
}
const createItemState = () => {
const state = proxy(
initialState, // not a good idea
addItem: () => state.ideas.push(createIdeaState())
)
return state
} I'm not sure if this a correct interpretation or not. I haven't had a chance to play around with it or look into the new version yet. I do agree that the description / examples could be a bit less ambiguous. |
Beta Was this translation helpful? Give feedback.
-
This is very confusing:
It is unclear what "reusing
obj
" means, it is not defined in the rest of the documentation.It is further unclear what "reusing the object" means. The documentation shows examples of setting attributes on
state
, and it's unclear how settingobj
to a new value won't work.Beta Was this translation helpful? Give feedback.
All reactions