Skip to content

URL can't update accordingly #25

@xwenc

Description

@xwenc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions