van icon indicating copy to clipboard operation
van copied to clipboard

Example for use with a global/unified state machine

Open tracker1 opened this issue 1 year ago • 1 comments

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.

tracker1 avatar May 25 '23 18:05 tracker1

Yep I was wondering if there were plans for a context api or if van.state() can be shared globally like preact signals.

pyrossh avatar May 26 '23 16:05 pyrossh

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

brettearle avatar May 30 '23 02:05 brettearle

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
      }
    })
  }
}

enpitsuLin avatar May 31 '23 10:05 enpitsuLin

Zustand is very lightweight and has an extremely simple api in vanilla with zustand/vanilla. Create a store useStore = createStore((set) => ({a: null, b: false,...})) and just call useStore.getState().a to read from the store and useStore.setState().a = 1 to update it. If you have multi-page navigation, then you will inject fresh components so the pattern (context)=>(props)=> [dom elts] used here is enough: you pass van.state in the context and just use it. I used this exact pattern here and it just works.

ndrean avatar Jun 05 '23 16:06 ndrean

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):

https://vanjs.org/demo#todo-app

Tao-VanJS avatar Oct 01 '23 21:10 Tao-VanJS

Closing the issue as the example is provided in https://github.com/vanjs-org/van/issues/12#issuecomment-1742201991.

Tao-VanJS avatar Nov 03 '23 18:11 Tao-VanJS