Skip to content
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

Closed
tracker1 opened this issue May 25, 2023 · 6 comments
Closed

Example for use with a global/unified state machine #12

tracker1 opened this issue May 25, 2023 · 6 comments

Comments

@tracker1
Copy link

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.

@pyrossh
Copy link

pyrossh commented May 26, 2023

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

@brettearle
Copy link

brettearle commented May 30, 2023

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

@enpitsuLin
Copy link
Contributor

enpitsuLin commented May 31, 2023

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

@ndrean
Copy link

ndrean commented Jun 5, 2023

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.

@Tao-VanJS
Copy link
Member

Tao-VanJS commented Oct 1, 2023

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
Copy link
Member

Closing the issue as the example is provided in #12 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants