-
-
Notifications
You must be signed in to change notification settings - Fork 252
How to organize actions
Noitidart edited this page Nov 10, 2021
·
5 revisions
Valtio is unopinionated about organizing actions. Here's some recipes to show various patterns are possible.
Note This way is preferred as it is better for code splitting.
import { proxy } from 'valtio'
export const state = proxy({
count: 0,
name: 'foo',
})
export const inc = () => {
++state.count
}
export const setName = (name) => {
state.name = name
}
import { proxy } from 'valtio'
export const state = proxy({
count: 0,
name: 'foo',
})
export const actions = {
inc: () => {
++state.count
},
setName: (name) => {
state.name = name
},
}
export const state = proxy({
count: 0,
name: 'foo',
inc: () => {
++state.count
},
setName: (name) => {
state.name = name
},
})
export const state = proxy({
count: 0,
name: 'foo',
inc() {
++this.count
},
setName(name) {
this.name = name
},
})
class State {
count = 0
name = 'foo'
inc() {
++this.count
}
setName(name) {
this.name = name
}
}
export const state = proxy(new State())