1

I am trying to call a function from componentDidMount which sets the State but keep coming across an error of

Uncaught ReferenceError: setPanelState is not defined

Below is the code...

export default class Patient extends React.Component {
  constructor(props) {
    super(props);
    autoBind(this);

    this.state = {
     PATIENT: [],
     COMPPROPS: [],
    };

    this.setPanelState = this.setPanelState.bind(this);
  }

    setPanelState(activity) {
          this.setState({COMPPROPS: [{compName:'Overview', compState:'Edit'}]});
    }

    componentDidMount() {   
       //handles chat commands and if the command is update patient the Overview panel should change to editable
       this.directLine.activity$
        .filter(function (activity) {
          return activity.type === 'event' && activity.value === 'Update Patient';
       })
       .subscribe(function (activity) {
         setPanelState(activity);   
       })
  }

I have tried make setPanelState a function outside of the class as opposed to a method but I get an error there as well.

Any thoughts?

0

4 Answers 4

2

Since you're using ES6 classes I assume you have it all set up.

Use arrow functions that bind this automatically

To learn more about arrow functions see this

.subscribe((activity) => {
    this.setPanelState(activity);   
})

Your component would look like this:

 export default class Patient extends React.Component {
  constructor(props) {
    super(props);
    autoBind(this);

    this.state = {
     PATIENT: [],
     COMPPROPS: [],
    };

    this.setPanelState = this.setPanelState.bind(this);
  }

    setPanelState(activity) {
          this.setState({COMPPROPS: [{compName:'Overview', compState:'Edit'}]});
    }

    componentDidMount() {   
       //handles chat commands and if the command is update patient the Overview panel should change to editable
       this.directLine.activity$
        .filter((activity) => {
          return activity.type === 'event' && activity.value === 'Update Patient';
       })
       .subscribe((activity) => {
         this.setPanelState(activity);   
       })
  }
Sign up to request clarification or add additional context in comments.

1 Comment

This appears to have done it! I seem to struggle with arrow functions and the use of this. Appreciate the help!
0

Use this.setPanelState(activity) and remember to maintian the context .As you are non ES6 arraow function . save the context outside var that=this and access that variable inside

Comments

0

In your componentDidMount method call setPanelState using this.setPanelState

you can also use a better format:

.subscribe(this.setPanelState)

If you put setPanelState outside the class and call it, it won't work, unless it is defined inside another class where you can use setState.

Comments

0

change to

 this.setPanelState(activity)

at the end.

1 Comment

I have tried this but i get the error "Uncaught TypeError: this.setPanelState is not a function"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.