cpunion/react-actioncable-provider

URL can't update accordingly

xwenc opened this issue · 0 comments

xwenc commented

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)
 },