-
I saw that you can do this in the documentation import { derive } from 'valtio/utils'
// create a base proxy
const state = proxy({
count: 1,
})
// create a derived proxy
const derived = derive({
doubled: (get) => get(state).count * 2,
})
// alternatively, attach derived properties to an existing proxy
derive({
tripled: (get) => get(state).count * 3,
}, {
proxy: state,
}) This only shows 1 level of proxy , I have a state which looks something like const state = proxy({
// ...
currentMenu: {
multiplier: 1,
items: [],
// I want to add a `totalPrice` derived property from `currentMenu.multiplier` and `currentMenu.items`
}
// ...
}) The What is the semantic way do do this in my case? |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Aug 28, 2021
Replies: 1 comment 3 replies
-
Assuming you don't modify derive({
totalPrice: (get) => {
const currentMenu = get(state.currentMenu)
return currentMenu.multiplier * currentMenu.items.reduce((p, c) => p + (c.selected ? c.price : 0), 0)
},
}, {
proxy: state,
}) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
geocine
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming you don't modify
currentMenu
reference, doesn't this work?