i have a simple mobx store :
class Test {
data;
constructor() {
this.data = { "1": { user: { name: "user0" } } };
makeAutoObservable(this);
}
get getData() {
return this.data;
}
changeData1() {
const newObj = { user: { name: "user1" } };
this.data["1"] = newObj;
}
changeData2() {
const newObj = { user: { name: "user2" } };
const item = this.data["1"];
item.user.name = newObj.user.name;
}
changeData3() {
const item = this.data["1"];
const newObj = { user: { name: "user3" } };
this.changeObjUtil(item, newObj);
}
changeObjUtil(obj1, obj2) {
obj1 = obj2;
}
}
if i will use the changeData1 and changeData2 actions it will trigger the computed and it will re render the react component but when using changeData3 action with changeObjUtil func it will not render even due object are passing as a reference to a function
any explanation ?
React Component :
const t = new Test();
export default observer(function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button onClick={() => t.changeData1()}>changeData1</button>
<button onClick={() => t.changeData2()}>changeData2</button>
<button onClick={() => t.changeData3()}>changeData2</button>
<div>
{Object.keys(t.getData).map((key) => {
const item = t.getData[key];
return <div key={key}>{item.user.name}</div>;
})}
</div>
</div>
);
});
thanks !