Skip to content

Behavior of Valtio when adding new objects into state #305

Closed Answered by dai-shi
CharlieDigital asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for trying valtio!

I think I understand the reason why because the instance retrieved from appState is proxied.

Exactly.

So my followup question is what is whether there is a better pattern in this case?

I would do this:

import { appState } from 'store/AppState'
const employee = proxy(new Employee());
appState.employees.push(employee);
employee.compensation = '$100,000';

So, valtio primitives are for function style.

Perhaps the better pattern here is to treat the appState like a factory

I think you can do it on your end, creating such a factory function.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@CharlieDigital
Comment options

@dai-shi
Comment options

@CharlieDigital
Comment options

Answer selected by CharlieDigital
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants