0

I'm trying to update a state value from within a method. When I .log above the setState the value is correct but afterwards I get an undefined. I should be getting the userObject. The data is passed to the method correctly, just not sure why the value isn't being set?

toggleModal = (user) => {
var userData = {};
if (user === null) {
  userData = {}
} else {
  userData = user
}
this.setState({
  isOpen: !this.state.isOpen,
  dataType: 'user',
  user: userData
});
console.log(this.state.user);
}

When I pass the value to another component the value is marked as undefined and not null or an empty object.

user7684436
  • 697
  • 14
  • 39

1 Answers1

1
this.setState({
  isOpen: !this.state.isOpen,
  dataType: 'user',
  user: userData
},()=>{console.log(this.state.user)});
egorchik
  • 531
  • 1
  • 4
  • 12