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

Shared State Across Islands? #1093

Closed
JacobWeisenburger opened this issue Mar 28, 2023 · 17 comments
Closed

Shared State Across Islands? #1093

JacobWeisenburger opened this issue Mar 28, 2023 · 17 comments

Comments

@JacobWeisenburger
Copy link

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?

@joaofreires
Copy link

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";

@JacobWeisenburger
Copy link
Author

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>
}
error: Uncaught TypeError: Cannot read properties of undefined (reading '__H')
    at f (https://esm.sh/stable/[email protected]/deno/hooks.js:2:189)
    at P (https://esm.sh/stable/[email protected]/deno/hooks.js:2:323)
    at T (https://esm.sh/stable/[email protected]/deno/hooks.js:2:292)

@joaofreires
Copy link

joaofreires commented Mar 29, 2023

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 preact is on your deno.json.
You will have something like:

"compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
}

And in your import_map.json:

"imports": {
    ....
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/",
    "preact-render-to-string": "https://esm.sh/*[email protected]",
    "@preact/signals": "https://esm.sh/*@preact/[email protected]",
    "@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
    ...

@JacobWeisenburger
Copy link
Author

Could you try to move the state declaration to inside the component?

I'm wanting a store that can be used in any island.

@joaofreires
Copy link

joaofreires commented Mar 29, 2023

You might consider using Preact Signals: https://preactjs.com/guide/v10/signals/#managing-global-app-state
Or Contexts: https://preactjs.com/guide/v10/context

@JacobWeisenburger
Copy link
Author

so there's no way to use zustand or jotai?

@JacobWeisenburger
Copy link
Author

You might consider using Preact Signals: https://preactjs.com/guide/v10/signals/#managing-global-app-state

how do I use this with typescript?

import { signal } from '@preact/signals'

const count = signal( 0 )
// const count: any

@joaofreires
Copy link

so there's no way to use zustand or jotai?

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
I hope it helps you.

@czottmann
Copy link

@joaofreires Thanks for the kind words! Was it this one? https://zottmann.org/2022/07/31/how-to-use.html

@JacobWeisenburger
Copy link
Author

JacobWeisenburger commented Mar 29, 2023

@joaofreires @czottmann
statery is not working for me. Please help.

/** @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>
}
error: Uncaught TypeError: pragma cannot be set when runtime is automatic at file:///C:/_Software/project/islands/InitStore.tsx:1:1
  await import(entrypoint);
  ^
    at async dev (https://deno.land/x/[email protected]/src/dev/mod.ts:187:3)
    at async file:///C:/_Software/project/dev.ts:18:1

@JacobWeisenburger
Copy link
Author

JacobWeisenburger commented Mar 30, 2023

I'm not sure if this will work for everything, but at first glance, this seems to work
How to use some of Zustand in Deno Fresh

@joaofreires
Copy link

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.

@JacobWeisenburger JacobWeisenburger changed the title State Management? Shared State Across Islands? Apr 3, 2023
@jiawei397
Copy link
Contributor

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.

@tunnckoCore
Copy link

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?

@OldManFroggy
Copy link

@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

https://preactjs.com/blog/introducing-signals/

@deer
Copy link
Contributor

deer commented Jun 28, 2023

@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.

@marvinhagemeister
Copy link
Collaborator

Good call, yeah I think this can be closed.

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

8 participants