SvenKirschbaum / react-stomp-hooks

This repository contain a react library which enables simple access to stomp subscriptions via hooks.
MIT License
63 stars 13 forks source link

Multiple rendering #6

Closed permmy closed 2 years ago

permmy commented 2 years ago

Hey, I have noticed when using redux to manage message state or any redux action within <StompSessionProvider></StompSessionProvider>, stomp is disconnecting and then reconnect when state changes or redux action is executed. Could you check on this. This is not happening when using react useState hook.

parent-component action
SvenKirschbaum commented 2 years ago

Hello!

I have personally never used redux before, but from your screenshots i don't think it is the cause of the issue here.

The props you set in the StompSessionProvider are passed on to the internal stompjs client. When you change any of them the stompjs client is destroyed and recreated with the new options, to create consistent behavior. If the containing component of your example rerenders, the callbacks are regenerated, and are not reference equal to their predecessors, triggering the recreation and therefore the reconnect. You can prevent this in your example by wrapping the individual callbacks in a useCallback hook.

I think that this behavior is potentially unintuitive. In general it would be possible to update all options on the existing client, but whether that is better depends very much on the specific property. For the callbacks in your example i think it would be preferable, but e.g. setting the connect Headers for authentication purposes taking effect only after a reconnect, e.g. after a network problem would be unintuitive too.

I will think about possible alternate solutions for this, and then maybe update the behavior in a future version.

If you need further assistance, or have any further questions or opinions on this, feel free the post a reply.