Map not behaving as observable
How to properly use types.map(model) and iterate?
- [x] I've checked documentation and searched for existing issues
- [ ] I tried the spectrum channel
I have following action in that is fetching an array of objects from my API. I am attempting to only update the polls that need to be updated in the mobx state based on the updatedAt date that is a property on my PollModel. I am performing this action in componentDidMount of my component that is rendering these Polls as a list.
When I start the application (empty state), it will fetch the data properly, create the Poll models and insert them into the map properly but when I attempt to iterate over the map in my React component it is always an empty array. Does the function self.polls.set(...) not cause an update in my mobx store? I am just trying to understand why the map is not behaving in an observable manner? Is Array.from(store.someMap.values()).map() the correct way to perform this iteration in my React component?
HomeStore.js
export const HomeStore = types
.model("HomeStore", {
polls: types.optional(types.map(PollModel), {}),
})
.views(self => ({}))
.actions(self => ({
fetchHomeFeed: flow(function*() {
const { data, errors, loading } = yield client.query({
query: HomeFeedQuery,
fetchPolicy: "network-only"
});
if (loading) {
console.log("loading polls");
}
if (errors) {
console.log("error fetching polls");
}
if (data) {
// only update/create polls that need to be updated/created
data.polls.forEach(fetchedPoll => {
// if we already have poll in state
if (self.polls.has(fetchedPoll.id)) {
let statePoll = self.polls.get(fetchedPoll.id);
// update if data is "old"
if (moment(fetchedPoll.updatedAt).isAfter(statePoll.updatedAt)) {
let pollOptions = [];
if (fetchedPoll.options.length > 0) {
pollOptions = fetchedPoll.options.map(option => {
// create option model
});
}
const newPoll = PollModel.create({
// create poll model
});
self.polls.set(newPoll.id, newPoll);
}
// otherwise we have the latest version of that poll
} else {
// create poll in state
let pollOptions = [];
if (fetchedPoll.options.length > 0) {
pollOptions = fetchedPoll.options.map(option => {
return OptionModel.create({
// create option model
});
});
}
const newPoll = PollModel.create({
// create poll model
});
self.polls.set(newPoll.id, newPoll);
}
});
}
})
}));
HomeFeed.js
class HomeFeed extends React.Component {
componentDidMount() {
this.props.homeStore.fetchHomeFeed();
}
render() {
const { homeStore } = this.props;
return (
<div className="feed-container">
<FeedHeader />
<div className="divider" />
<div className="feed-list-container">
<ul className="feed-list">
{Array.from(homeStore.polls.values()).map(poll => {
// display the poll
)}
</ul>
</div>
</div>
);
}
}
export default inject("homeStore")(observer(HomeFeed));
Was able to figure it out. I needed to use values(someMap)map(...) instead and I needed to make more components observers
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.