observer HOC

    Along the observer capability, the React.memo is applied to the component. The general idea is, that observed components rarely need to re-render based on a complex props.

    If component wrapped to observer depends on updates of legacy context, the underlying React.memo will effectively prevent that update to propagate.

    1. import { observer, useLocalStore } from 'mobx-react' // 6.x or mobx-react-lite@1.4.0
    2. export const Counter = observer<Props>(props => {
    3. const store = useLocalStore(() => ({
    4. count: props.initialCount,
    5. store.count += 1
    6. },
    7. }))
    8. return (
    9. <span>{store.count}</span>
    10. <button onClick={store.inc}>Increment</button>
    11. </div>
    12. )
    13. })