-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for use with a global/unified state machine #12
Comments
Yep I was wondering if there were plans for a context api or if van.state() can be shared globally like preact signals. |
Could this be achieved with Module import/export. in a svelteKit style way? main.js imports state required from store.js? On my phone but will test it this evening |
I have some quite simple way to use jotai with vanjs, but didn't test in most case, it works on my simple todomvc app import type { State } from './lib/van'
import { Atom, WritableAtom, atom, createStore } from 'jotai/vanilla'
type UseAtom = {
<Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>): State<Value>
<Value>(atom: Atom<Value>): State<Value>
}
function vanjsJotaiFactory(): UseAtom {
const store = createStore()
return <Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result> | Atom<Value>) => {
const atomState = van.state(store.get(atom))
return new Proxy(atomState, {
get(state, prop) {
if (prop === 'val')
return store.get(atom)
//@ts-expect-error
return state[prop]
},
set(state, prop, newValue: Value) {
//@ts-expect-error
state[prop] = newValue
if (prop === 'val' && 'write' in atom && newValue !== store.get(atom)) {
//@ts-expect-error
store.set(atom, newValue)
}
return true
}
})
}
} |
|
Hi @tracker1, With VanJS's builtin state management, you can build your app based on a single global state. Here is an example (state persistence is supported as well): |
Closing the issue as the example is provided in #12 (comment). |
For Advanced Examples, it would be nice to see a way to use this against a state engine like Zustand, Jotai, Valtio, Redux or MobX. Since being able to integrate a larger shared state would be beneficial for use in larger application(s) development.
The text was updated successfully, but these errors were encountered: