Comments (6)
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.
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.
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.
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.
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.
Closing the issue as the example is provided in #12 (comment).
from van.
Related Issues (20)
- Lifecycle event: mount and unmount HOT 1
- Error on when using method for Date data in VanX reactive (and possibly all class instance). HOT 4
- Download table broken HOT 2
- how to manually re-render? HOT 8
- Clear reactive array HOT 18
- Script children should not be escaped HOT 6
- Intersection observer HOT 11
- Can createDocumentFragment be used directly? HOT 2
- Smallest reactive library HOT 6
- A way to make VanJS even smaller HOT 2
- iOS usecase?
- Array type for properties values HOT 2
- A new version of the TodoList in Javascript with editable input fields
- vanjs-jsx Fragment missing HOT 6
- [vanjs-jsx] Add missing ./src/jsx-runtime.js HOT 3
- [vanjs-jsx] style property no more handled HOT 1
- Bad support on conditional rendering when return nulls HOT 4
- HTML Character Entity for " " not correctly loading rendering when using span() tag. HOT 2
- Element not rendering properly HOT 5
- Support JSR HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from van.