0

this are my initial state:

this.state = {
  requiredError: false,
  addressSelectError: false,
  recipientError: false,
  open: false,
};

and this is where my problem is:

processCheckout () {
    this.setState({
      requiredError: true, 
      addressSelectError: true,
      recipientError: true,
    }, () => {
      console.log('required error', requiredError);
      console.log('address error', addressSelectError);
      console.log('recipient error', recipientError);
    })
}

I have set them all to true but the console still logs false:

enter image description here

I have used the callback but it still does not change. any help?

I am calling the processCheckout() function on onClick() of a button.

EDIT: I have tried console logging in render, and all of them are true

2
  • Can you show the full code of where you are updating the state Commented Apr 13, 2019 at 4:59
  • Where have you defined requiredError, addressSelectError and recipientError? Commented Apr 13, 2019 at 5:13

2 Answers 2

2

Change your function to an arrow syntax so it has access to the global state.

processCheckout= () => {
    this.setState({
      requiredError: true, 
      addressSelectError: true,
      recipientError: true,
    }, () => {
      console.log('required error', this.state.requiredError);
      console.log('address error', this.state.addressSelectError);
      console.log('recipient error', this.state.recipientError);
    })

Or bind it at the constructor like

constructor(props) {
    super(props);

    // This binding is necessary to make `this` work
    this.processCheckout = this.processCheckout.bind(this);
  }

You can read more about it here

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried console logging in render, and all of them are true but at the console log at the callback it is not reflected
1

Without seeing the entire code I assume you've declared the variables requiredError, addressSelectError and recipientError somewhere in your class:

In the callback, try to access this.state directly

processCheckout() {

    this.setState(
      {
        requiredError: true,
        addressSelectError: true,
        recipientError: true
      },
      () => {
        console.log("required error", this.state.requiredError);
        console.log("address error", this.state.addressSelectError);
        console.log("recipient error", this.state.recipientError);
      }
    );
  }

2 Comments

I have declared them like this const { requiredError, addressSelectError, recipientError } = this.state; before the setState and I thought that's the same. but when I tried your answer it works, why is that? and also thanks!
When you declare const { requiredError, addressSelectError, recipientError } = this.state; in your processCheckout method they reference the state at the current state. In this case requiredError, addressSelectError, recipientError are all false since they are defined false in your constructor method.

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.