4.3. Second spell: observer
The counter doesn't change, because we need to tell MobX that this component uses our store. We do that with the second spell, observer:
import { observer } from 'mobx-react-lite';
import { store } from './Store';
import './styles.css';
const Counter = observer(() => {
const { value, add, reset } = store;
return <div className="counter">
{/* ... */}
</div>;
});
observer remembers which state the component read while rendering and renders it again when that state changes. The console.log in add has done its job, so delete it.
And this is all the magic you need to start working with MobX. But it isn't everything.
Need a hint?
- observer wraps the whole arrow function: const Counter = observer(() => { ... });
Click the buttons. Now it works.