Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is context provider in SSR mode necessary?#87

Answeredbylostpebble
bolu61 asked this question inQ&A
Discussion options

Since pullstate implements an alternative to the context API, is there a way to avoid using it in SSR?

You must be logged in to vote

If you are manipulating the state during the rendering on the server-side and need to hydrate that state on the client side (which is generally always the case) - then there is no other way to go about it than using context.

Any library which does SSR and is manipulated during a render needs to have context- there's no way around it, because each separate user "request" on a server needs to maintain a unique state, which can't be controlled globally like we do on the client (otherwise all requests share the same state and clash / overwrite each other)- hence context is required.

If you are not manipulating the state during rendering on the server, then you don't need the context provider.…

Replies: 1 comment 2 replies

Comment options

If you are manipulating the state during the rendering on the server-side and need to hydrate that state on the client side (which is generally always the case) - then there is no other way to go about it than using context.

Any library which does SSR and is manipulated during a render needs to have context- there's no way around it, because each separate user "request" on a server needs to maintain a unique state, which can't be controlled globally like we do on the client (otherwise all requests share the same state and clash / overwrite each other)- hence context is required.

If you are not manipulating the state during rendering on the server, then you don't need the context provider. But I'd recommend just putting it in anyway because you might run into weird issues in the future.

You must be logged in to vote
2 replies
@bolu61
Comment options

For the sake of discussion, is it possible to implementPullStateCore without React's context API? For example using a simple map of all the individual stores. Since they are already reactive, we don't need context consumers for hydration (Correct me if I'm wrong). I'm asking because I personally don't like wrapping an app with a context provider component.

Something along the lines of:

// redacted

Edit: Nvm, I just answered my own question. It would in fact lead to very ugly code.

@lostpebble
Comment options

I feel you, I also had a big aversion to it- and tried my hardest to not have to use it with Pullstate. But when it comes down to it, and after much testing and thought, it just isn't something that can be done- conceptually, React needs a way to hold state during render on the server- which we pass on to the client- and context is what provides that.

I've come to understand the need for it and I don't dislike it as much as I used to though.

Answer selected bybolu61
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@bolu61@lostpebble

[8]ページ先頭

©2009-2025 Movatter.jp