You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OK, I just found that the above issue was discussed already in issue #24. I think this deserves more explanation in the README, but for now, I'll cover my findings here.
Destructuring props outside of JSX snippets (either in the arguments list or in the render function body) doesn't work:
// This does NOT work; the component will not re-render when props.count changes.exportdefaultfunctionCountView(props: CountViewProps){const{count}=props;return<divclassName="counter">{count}</div>;}
Destructing props within a JSX snippet does work:
// This DOES work; changes to props.count will be rendered in the browser.exportdefaultfunctionCountView(props: CountViewProps){return<divclassName="counter">{props.count}</div>;}
I can understand why, since the CountView function is only run once in each lifecycle, and only the JSX snippets get observed and re-evaluated after that. But still, this was confusing as a newcomer!
Are there more docs for mobx-jsx beyond the README?
I started with your functional counter example here: https://codesandbox.io/s/mobx-counterfunctions-3sqv1
First I converted it to TypeScript and got that working. Next, I abstracted the counter div to a separate component:
Then I refactored the
App
component to useCountView
and to also log the count to console:The console shows that the count is incrementing, but the count in the browser doesn't update — it's stuck at 0.
Why doesn't the
CountView
component re-render whenstate.count
changes?The text was updated successfully, but these errors were encountered: