Child component without observer tracking state updates #3592
Replies: 3 comments 1 reply
-
Parent will track this change as per https://mobx.js.org/understanding-reactivity.html, child doesn't track the change, it simple re-renders because it renders whenever the parent re-renders, which is the default behavior of React unless you throw a |
Beta Was this translation helpful? Give feedback.
1 reply
-
The docs are dealing with the more common issue of people wondering why
their child components are NOT updating with mobx.
On Sat, Dec 24, 2022 at 8:08 AM Rajat Mondal ***@***.***> wrote:
@mweststrate <https://github.com/mweststrate>
I understand the re-render part, as the parent is re-rendering, child will
also that but then what is the point behind
it is written in the docs !!!
—
Reply to this email directly, view it on GitHub
<#3592 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABCW4TRE3ZTJDKU3RZNKMLWO37VTANCNFSM6AAAAAATILURMA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
--
-Matt Ruby-
***@***.***
|
Beta Was this translation helpful? Give feedback.
0 replies
-
The general point of MobX is that in the typical case, you *don't* want the
re-render e.g. if you have 1000 items, and 1 changes, you want only that
specific item to re-enter, not the parent, not the 999 other ones, and that
is what observer gets you. I recommend to watch any of the talks or
tutorials available, that is typically where they start.
…On Sat, 24 Dec 2022, 15:08 Rajat Mondal, ***@***.***> wrote:
@mweststrate <https://github.com/mweststrate>
I understand the re-render part, as the parent is re-rendering, child will
also that but then what is the point behind
it is written in the docs !!!
—
Reply to this email directly, view it on GitHub
<#3592 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBDUU5MDVQ24GO5ZIETWO37VPANCNFSM6AAAAAATILURMA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
hyvip-ai
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am new to MOBX and according to documentation while using it with react, if a Child component is not wrapped in observer, it won't track any changes in state, even though the Parent is wrapped in Observer.
But the child component is tracking updates even if, it is not wrapped and the parent is wrapped.
Code:
App.jsx -
import { store } from "./store";
import { observer } from "mobx-react";
function App() {
return (
<>
<input onChange={(e) => store.setNewTodo(e.target.value)} />
</>
);
}
function Comp({ data }) {
return (
<>
parent wrapped in observer:{data.newTodo}
</>
);
}
const CompObserver = observer(Comp);
function Child({ data }) {
console.log(JSON.stringify(data));
return
child not wrapped in observer:{data.newTodo}
;}
Store:
import { makeAutoObservable } from "mobx";
class Store {
todos = [];
newTodo = "";
constructor() {
makeAutoObservable(this);
}
setNewTodo(data) {
this.newTodo = data;
}
}
export const store = new Store();
Expected Output:
parent wrapped in observer: something
child not wrapped in observer:
Actual output:
parent wrapped in observer: something
child not wrapped in observer: something
export default App;
codesandbox link: https://codesandbox.io/s/mobx-vq9g28?file=/src/store.js:0-228
Beta Was this translation helpful? Give feedback.
All reactions