I have created a store using mobx
as follows:
import {extendObservable} from 'mobx';
class InfluencerFeedStore {
constructor() {
extendObservable(this, {
data: []
});
}
setData(items = []) {
this.data = items;
}
}
export default new InfluencerFeedStore();
And I then observe that store in my React view:
import React from 'react';
import {observer} from 'mobx-react';
import FeedItem from './FeedItem';
import InfluencerFeedStore from '../../core/stores/InfluencerFeed';
import './style.css';
const generateItems = () => {
return InfluencerFeedStore.data.map((item, i) => (
<FeedItem key={`feeditem-${i}`} {...item} />
));
};
const Feed = () => (
<div className="Feed vertical-scroll-flex-child">
{generateItems()}
</div>
);
export default observer(Feed);
On first render, my Feed
view works just fine (although there are no items in the InfluencerFeedStore.data
array).
If I later load items by calling InfluencerFeedStore.setData()
, React will correctly attempt to re-render the Feed
view (because it noticed the mobx observable updated)... but I get an error complaining that InfluencerFeedStore.data.map is not a function
.
From reading through the mobx
docs, I gather that re-assigning my data
property is problematic because it's an array (whereas other data types like strings "just work"). Can anyone tell me what I'm doing wrong here?