I use a class to create an object in react component but when I try to set the state to the new object it doesn't rerender the component
Class:
class persons {
constructor(list = {}) {
this.list = list
}
getPersons() {
return this.list
}
addPerson(person) {
this.list[someId] = person
}
}
in component when I add new person and set the state to the new list it didn't rerender the component like that:
//...
const [personsList , setPersonsList] useState({})
const persons = new Persons(personsList)
persons.addPerson({name: "Mike"})
setPersonsList(persons.list)
//it doest also rerender with that
persons.addPerson({name: "Mike"})
setPersonsList(persons.getPersons)
//..
Although when I log persons.list or persons.getPersons() It shows the new list with the added person