react-actioncable-provider icon indicating copy to clipboard operation
react-actioncable-provider copied to clipboard

Fix ActionCableConsumer component unmounting/remounting on parent component rerender

Open ihollander opened this issue 5 years ago • 14 comments

This fixes the issue of the ActionCableConsumer component being unmounted and remounted every time the parent component rerenders.

Since a new ActionCableConsumer class component was being created every time the forwardRef function runs, it was continually unmounting and mounting new versions of the component. This would also have the effect of unsubscribing and re-subscribing to the socket connection each time the parent rerenders, not just when the parent component unmounts.

I moved the createReactClass function outside of the forwardRef function and the component now behaves as expected.

Fixes #21

ihollander avatar May 17 '19 14:05 ihollander

I had the same problem. Was super annoying and was stuck debugging for 4 days with no working code. Switched back to the built in actioncable in react and it works so much easier. Just pass the cable as a prop down to any child comportment that needs to use sockets. `

csx773 avatar May 31 '19 01:05 csx773

@csx773 - do you have an example?

cseeger avatar Jun 06 '19 01:06 cseeger

Would love to see this merged; seeing the exact same issue as described in our project. It's preventing messages from being transmitted because the component is re-rendering at the exact moment a message is sent, so it never transmits.

chancegraff avatar Jun 07 '19 17:06 chancegraff

I also would suggest this to be merged. It fixes the issue, and I'm currently using @ihollander branch in production

brunoalano avatar Aug 19 '19 17:08 brunoalano

I'm using the fork as well, with an extension of also adding in the ability for a component to re-render if the channel is changed. I didn't create a PR since I'm not sure if that's an isolated feature that our app needed, but if anyone would like to see it, it's here:

https://github.com/jordanhudgens/react-actioncable-provider

jordanhudgens avatar Aug 19 '19 17:08 jordanhudgens

I'm using @jordanhudgens' fork in my app currently! Thanks!

xaviablaza avatar Sep 02 '19 22:09 xaviablaza

When will this be closed and merged?

UtamaruMonster avatar Oct 23 '19 09:10 UtamaruMonster

this is very useful, would love to see it merged :)

meshaabi avatar Apr 05 '20 18:04 meshaabi

+1 here

ssettle3 avatar Apr 08 '20 23:04 ssettle3

this has been merged here https://github.com/JackHowa/react-actioncable-provider/pull/6 -- also without react lifecycle warnings

npm install --save @thrash-industries/react-actioncable-provider
# OR
yarn add @thrash-industries/react-actioncable-provider

thanks @marcelkooi for notifying me.

Thanks @ihollander for the work. I added you as a collaborator to the project

JackHowa avatar Apr 13 '20 23:04 JackHowa

this has been merged here JackHowa#6 -- also without react lifecycle warnings

npm install --save @thrash-industries/react-actioncable-provider
# OR
yarn add @thrash-industries/react-actioncable-provider

thanks @marcelkooi for notifying me.

Thanks @ihollander for the work. I added you as a collaborator to the project

I am trying to use your forked repo in order to solve this same render problem but now I am facing a diferent one

const FullGameTable = () => {

  const [participants, setParticipants] = useState([]);

  const handleReceivedGameDebug = (props) => {
    console.log(props, participants)
    setParticipants(props.participants)
  }

  return (
    <div>
      <ActionCableConsumer
        channel={{ channel: "GamesChannel" }}
        onReceived={handleReceivedGameDebug}
      />
    </div>
  );
};

Basically my setState hook function seems to not be called anymore, once participants inside handleReceivedGameDebug is always empty. Although, if I check participants through React Dev Tools, or even outside handleReceivedGameDebug fn, it is updated.

joaomosm avatar Apr 23 '20 07:04 joaomosm

huh @joaomosm, would be happy to take a look if I can with an example including the sockets call? feel free to post in the issues on my extension

JackHowa avatar May 27 '20 00:05 JackHowa

Would love to see this merged

ryanflynndev avatar Sep 25 '20 21:09 ryanflynndev

I've published this and people are using my fork @ryanflynndev

JackHowa avatar Sep 26 '20 14:09 JackHowa