Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ActionCableConsumer component unmounting/remounting on parent component rerender #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ihollander
Copy link

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

@csx773
Copy link

csx773 commented May 31, 2019

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.
`

@cseeger
Copy link

cseeger commented Jun 6, 2019

@csx773 - do you have an example?

@chancegraff
Copy link

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.

@brunoalano
Copy link

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

@jordanhudgens
Copy link

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

@xaviablaza
Copy link

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

@UtamaruMonster
Copy link

When will this be closed and merged?

ramsy-leftclick added a commit to LeftClickBV/react-actioncable-provider-updated that referenced this pull request Nov 5, 2019
(implement: PR26, see
cpunion#26)
@meshaabi
Copy link

meshaabi commented Apr 5, 2020

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

@ssettle3
Copy link

ssettle3 commented Apr 8, 2020

+1 here

@JackHowa
Copy link

JackHowa commented Apr 13, 2020

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

@joaomosm
Copy link

joaomosm commented Apr 23, 2020

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.

@JackHowa
Copy link

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

@ryanflynndev
Copy link

Would love to see this merged

@JackHowa
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Web-socket unsubscribes and doesn't reconnect