I wish to show a modal dialog box (such as an alert()
) every time a Meteor subscription, tracked in React with withTracker
, changes.
I have tried using Tracker.autorun
to track changes but cannot work out where in the code to place it. It does not seem to work in the Component
constructor and runs every time if placed in render()
.
This is an outline of what my code looks like:
class Foo extends Component {
render() {
return (
<h1>Example Header</h1>
{ this.maybeShowAlert() }
);
}
maybeShowAlert() {
// ONLY if bar has been updated
alert('bar has changed');
}
}
export default withTracker(() => {
Meteor.subscribe('bar')
return {
bar: Bar.findOne({})
};
})(Foo);
Haven't used Meteor before, but if you want to do things in response to state/prop changes then componentDidUpdate()
is the lifecycle method for it. E.g.
componentDidUpdate(prevProps) {
if (this.props.bar !== prevProps.bar) {
// bar prop has changed
alert("bar changed");
}
}