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

URL can't update accordingly #25

Open
xwenc opened this issue Apr 10, 2019 · 0 comments
Open

URL can't update accordingly #25

xwenc opened this issue Apr 10, 2019 · 0 comments

Comments

@xwenc
Copy link

xwenc commented Apr 10, 2019

Url is dynamic in my project. when url changes, we found action cable can't update accordingly. So I found the problem is we should use the next props but not this props as an argument for componentWillMount in componentWillReceiveProps(https://github.com/cpunion/react-actioncable-provider/blob/master/lib/index.js#L41); Below is my code for reference:

componentWillMount: function () {
   this.connect(this.props);
},

componentWillUnmount: function () {
   if (!this.props.cable && this.cable) {
     this.cable.disconnect()
   }
 },

connect:  function (props) {
   console.log("action cable url on provider ============>", props.url)
   if (props.cable) {
     this.cable = props.cable
   } else {
     this.cable = actioncable.createConsumer(props.url)
   }
 },

 componentWillReceiveProps: function (nextProps) {
   // Props not changed
   if (this.props.cable === nextProps.cable &&
       this.props.url === nextProps.url) {
     return
   }
   // cable is created by self, disconnect it
   this.componentWillUnmount()
   // create or assign cable
   this.connect(nextProps)
 },
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

No branches or pull requests

1 participant