I have three components; Form, Preview & AppStore. Clicking a button in Form adds an item to the store. This seems to work fine except that the list in the Preview component isn't updating/re-rendering when the store changes even though it has an @observer decorator. What am I missing?
Form has a button and handler function that adds an item to the store:
@inject('AppStore')
@observer class Form extends React.Component{
handleAddItem= (item) =>{
const {AppStore} = this.props;
AppStore.addItem(item);
console.log(AppStore.current.items)
}
render(){
return(
<button onClick={() => this.handleAddItem('Another Item')}>Add Item</button>
)}
}
Preview maps through the items (I'm using a drag and drop hoc so my code might look a bit odd)
@inject('AppStore')
@observer class Preview extends React.Component
...
return(
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>)
...
return <SortableList items={AppStore.current.items} onSortEnd={this.onSortEnd} />;
Here is store:
import { observable, action, computed } from "mobx";
class AppStore {
@observable other = {name: '', desc:'', items: [ 'item 1', 'item 2', 'item 3'], id:''}
@observable current = {name: '', desc:'', items: [ 'item 1', 'item 2', 'item 3'], id:''}
@action addItem = (item) => {
this.current.items.push(item)
}
}
const store = new AppStore();
export default store;