preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.75k stars 91 forks source link

Not reactive when using with `preact-render-to-string` #569

Closed sransara closed 3 months ago

sransara commented 3 months ago

Environment

Describe the bug Are signals not supposed to reactive when used with preact-render-to-string's render? Am I doing something wrong or is there a bug?

To Reproduce

https://stackblitz.com/edit/preact-starter-template-t9utmr?file=Components.tsx

I have a signal and two components that share it. Rendered with preact-render-to-string. The expected run time environment is the server with no hydration.

Expected behavior

I was expecting that when zxcv Image is added, zxcv Ref will react to it and update the value to 4.

Thank you.

XantreDev commented 3 months ago

Hey. What is the use case for the feature? Signals are more about client side reactivity

sransara commented 3 months ago

I want to server side render a prose component, such that the inner tags can define and refer to each other and render by their occurrence number in the prose component.

For example:

<Prose>
    ....
    <Figure name="hello" />
    ....
     Refer to <Ref name="hello" />
    ....
</Prose>

Renders:

<article>
    <figure><a id="hello"><img src=... /><caption>Image 1. hello</caption>

    Refer to <a ref="#id">Image 1. hello</a>
</article>

Above works alright, until I have a forward reference where the Figure tag comes after Ref tag. Hence needing some sort of re-render. I was hoping to use signals to prevent the need to re-render the whole tree and just surgically update the subscribed components.

I was thinking, when preact-render-to-string's render is called, it will wait until subscribed components are updated and then render the final result.

sransara commented 3 months ago

Signals are more about client side reactivity

Considering that, what I'm trying seems out of scope. Thank you. I'll close this ticket.

XantreDev commented 3 months ago

As I see preact render to string even do not run effects. So probably it's one way renderer https://stackblitz.com/edit/preact-starter-template-tbszme?file=Components.tsx image

sransara commented 3 months ago

Right. Thanks for checking. I was just reading this article: https://codewithhugo.com/react-useeffect-ssr/ and the reasoning makes sense.