0

Here my state is not updated after using setState.log shows default value most of the time. Why is setState not consistent?

export default class Row extends Component {
  constructor(props) {
    super(props);

    // icons to listview (arrow icon)
    this.icons = {
      up: require("../../assets/ic_play_circle_down.png"),
      down: require("../../assets/ic_play_circle_up.png")
    };

    this.state = {
      title: props.title,
      // getting screen size (width and height)
      let: ({ width, height } = Dimensions.get("window")),

      // collapsed check the listview expanded or not if it is true: list view not expanded
      collapsed: true,

      isApproved: false,
      comment: "dummy comment"
    };
  }
  acceptApproval = () => {
    this.setState({ isApproved: true });
    console.log("Approved TICK " + this.state.isApproved);
    this.verifyApprovals();
  };

  regectApproval = () => {
    this.setState({ isApproved: false });
    console.log("Approved" + this.state.isApproved);
    this.verifyApprovals();
  };
zimmerrol
  • 4,872
  • 3
  • 22
  • 41
Rajitha Perera
  • 1,581
  • 5
  • 26
  • 42

1 Answers1

2

As you know setState is asynchronously, You should invoke your second function as a callback to setState

this.setState({ isApproved: true }, function(){
   this._setApproval(true)
  });

  };
_setApproval(){
console.log("Approved TICK " + this.state.isApproved);
    this.verifyApprovals();
}

  regectApproval = () => {
    this.setState({ isApproved: false } , function() {
    this._setRejection();
});

  };

_setRejection() {
console.log("Approved" + this.state.isApproved);
    this.verifyApprovals();
}
brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
Kishan Oza
  • 1,707
  • 1
  • 16
  • 38