setState and forceUpdate do not fire
krishnaglick opened this issue · 2 comments
If this is a known bug, or an expression of react on the server, feel free to close this ticket.
I have a generic loader component that uses a mix of SSR and component state in order to swap between loading, error, and success views. It functions fine in the client but does not work on the server due to setState calls seemingly being ignored. I tried with forceUpdate as well, but no luck there.
Is this something react-async-ssr can fix or provide in the future?
Hi @krishnaglick.
State cannot change over time on a server render. On the client side, the page lives for some time and so can react to events, change the DOM etc. On server side, the page is rendered once to HTML and then sent to the client. So, generally speaking, it doesn't make sense the change state during a render in the way you would on the client side.
Consequently setState
does not have any effect. I'm not familiar with forceUpdate
but I assume it's the same.
This library is intended to add support for Suspense
and React.Lazy
-style component/data loading on server side. So you'd need to be building your app in this style rather than using state to manage what's rendered while loading/loaded/failed. This, as I understand it, is one of the the purposes of React's Suspense feature - to remove the need to handle loading state yourself.
Supporting state on the server side is a whole other class of problem, and not one that I intend this tackle with this library. So I'm going to close this issue.
I hope this makes sense.
Thanks, this clears that up. I appreciate it!