Giter VIP home page Giter VIP logo

Comments (6)

enpitsuLin avatar enpitsuLin commented on July 30, 2024 2

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

from van.

pyrossh avatar pyrossh commented on July 30, 2024

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

from van.

brettearle avatar brettearle commented on July 30, 2024

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

from van.

ndrean avatar ndrean commented on July 30, 2024

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.

from van.

Tao-VanJS avatar Tao-VanJS commented on July 30, 2024

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

from van.

Tao-VanJS avatar Tao-VanJS commented on July 30, 2024

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

from van.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.