-
默认情况下,创建的model是全局单实例,全局共享: export const obs = model({
count: 0,
increment() {
obs.count ++
}
}) 如果希望以页面或者组件创建实例,如何处理,希望有类似于 zustand/context 的处理方式 或者说,推崇这种写法?: import { model, markRaw } from '@formily/reactive'
const Index = () => {
const m = useMemo(() => {
return markRaw(model({
count: 0,
increment() {
obs.count ++
}
}))
}, [])
return <View></View>
} |
Beta Was this translation helpful? Give feedback.
Answered by
bigmeow
Jun 19, 2023
Replies: 1 comment
-
我想到种写法,配合context和Provider: import { createContainer } from "unstated-next"
export const container = createContainer(() => {
const store = model({
count: 0,
count2: 1,
increment() {
store.count ++
},
increment2() {
store.count2 ++
},
})
return store
})
const Index = observer(() => {
const store = container.useContainer()
return <View>{store.count}</View>
})
const App = () => {
return <container.Provider><Index/></container.Provider>
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
bigmeow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我想到种写法,配合context和Provider: