-
Notifications
You must be signed in to change notification settings - Fork 668
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
Shared State Across Islands? #1093
Comments
Fresh does have compatibility with Preact. I would say for you to try the Preact hooks/signals as state manager. You can import direcly as: import { useState } from "preact/hooks"; |
This doesn't seem to work the way you have said. Can you help? import { useState } from 'preact/hooks'
const [ count, setCount ] = useState( 0 )
export default function Counter () {
return <button onClick={() => {
setCount( count => count + 1 )
}}>Count: {count}</button>
}
|
Could you try to move the state declaration to inside the component? import { useState } from 'preact/hooks'
export default function Counter () {
const [ count, setCount ] = useState(0)
return <button onClick={() => {
setCount(count => count + 1)
}}>Count: {count}</button>
} Also, you need to check if "compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
} And in your
|
I'm wanting a store that can be used in any island. |
You might consider using Preact Signals: https://preactjs.com/guide/v10/signals/#managing-global-app-state |
so there's no way to use zustand or jotai? |
how do I use this with typescript? import { signal } from '@preact/signals'
const count = signal( 0 )
// const count: any |
If I recall correctly, @czottmann wrote an excellent article about it (which I haven't been able to find and share here). However, there's also this discussion that might provide you with new ideas: #230 |
@joaofreires Thanks for the kind words! Was it this one? https://zottmann.org/2022/07/31/how-to-use.html |
@joaofreires @czottmann /** @jsx h */
import { h } from 'preact'
import {
makeStore, useStore
} from 'https://esm.sh/[email protected]?alias=react:preact/compat&[email protected]'
/* Make a store */
const store = makeStore( { count: 0 } )
export default function InitStore () {
const state = useStore( store )
return <div>{state.count}</div>
}
|
I'm not sure if this will work for everything, but at first glance, this seems to work |
Hey, I have implemented a lightweight store to share data across islands and potentially across pages (although I'm not yet sure if it's a good idea). The store is compatible with Preact hooks, so you only need to create a single store file and import the hooks from there. If you think it's worth checking out, you can find it at: https://github.com/joaofreires/FreshMint-Store Note: this is just a sketch, so please be careful when using it. |
I have used this module https://deno.land/x/[email protected]/mod.ts in my business, and it's okay, although I don't think it's the most elegant solution. |
I'm having similar issues. I'll try the suggested solutions, but. I'm playing with local & session storage, it seems the it's not persisted across pages and islands - meaning, when i read from there, it retirns null, but i can see them they actually sitting in the stores. Eg, i have a page which imports an island, the island sets some kv pairs, and then when i hit another page that includes another (or the same) island - the island cannot read the state that was written. Ideas? |
@tunnckoCore preact/signals should do what you need, I use preact signals myself to maintain app, and island state for a few large fresh apps currently, look at https://fresh-with-signals.deno.dev/ and the preact/signals site |
@marvinhagemeister, can this be closed? It seems like what Scott provided in the most recent comment solves this problem. Additionally, I have #1311 open to document this process. |
Good call, yeah I think this can be closed. |
I tried to use zustand but this happened: #1091.
Then I tried to use jotai and this happened: pmndrs/jotai#1854.
Is there a state manager that works with Fresh?
The text was updated successfully, but these errors were encountered: