I seem to be having some trouble with Meteor’s reactivity.
I have created a publication for my database:
Meteor.publish('menus', function publishMenus() {
return MenuCollection.find({ userId: this.userId });
});
I am then subscribing to this and printing the results in my React component:
export const MenuList = () => {
// Wait for menu subscription data
const [menus, isLoading] = useTracker(() => {
const sub = Meteor.subscribe('menus');
const menus = MenuCollection.find();
return [menus, !sub.ready()]
});
return (
<ul className="list-group">
{isLoading ? 'Loading...' :
menus.map(menu => {
return <MenuListItem key={menu._id} menu={menu} />
}
)}
</ul>
);
};
A parent component contains a form to add items to the collection. This parent component also then renders this child list component:
export const Menu = () => {
// Handle form submit - inset menu into db
const submit = data => {
Meteor.call('menus.insert', data, err => {
if (err) console.log(err);
});
};
return (
<>
<AutoForm schema={menuBridge} onSubmit={submit} />
<hr/>
<MenuList />
</>
);
};
However, I have to refresh the page for the newly added item to render in the list. I have the Meteor developer tools Chrome extension installed and the record updates and appears there - in the Minimongo section without having to refresh it.
I thought Meteor automatically handled this reactivity but some articles / answers suggest it doesn’t and that I need to use a package like meteor-reactive-publish or meteor-publish-composite? Other answers suggest the opposite - that I do not need an extra package and that Meteor should handle this.
What am I missing? Any help would be greatly appreciated