0

I am trying to reuse an object in different components so my initial obj. is empty but when i export it into my component and setState to obj., it doesn't clear the state. but when I console.log the obj., it comes back as expected. any idea why this is happening?

this.state = {
  name: '',
  lastName: '',
  age: 0,
  kids: 0
}

const myEmptyObj = {
  name: '',
  lastName: '',
  age: 0
}

// lets pretend this function has name, lastName, age, and kids with values
// this function should clear the state but update the kids to value 3. so everything    
// empty but the kids field

myFunctionClearState() {
  myEmptyObj.kids = 3;
  setState(myEmptyObj);
}
Karina
  • 23
  • 5
  • let's not pretend. Post the relevant portion of your actual code (including the mentioned `console.log`) – pilchard Apr 12 '22 at 22:11
  • see: [Updating an object with setState in React](https://stackoverflow.com/questions/43638938/updating-an-object-with-setstate-in-react) – pilchard Apr 12 '22 at 22:18

0 Answers0