It’s not sending everything as new. Ok, here’s a quick rundown on createContainer
:
We’ll make a container for MyComponent
. It looks like this, as an empty container that does nothing at the moment, but just to illustrate the reactivity of it:
import { createContainer } from 'meteor/react-meteor-data';
import MyComponent from './MyComponent';
export default createContainer(() => {
/*
* Anything reactive (Mongo, Session, ReactiveVar, etc) in this function will
* cause it to re-run and pass props into MyComponent.
*/
return {}
}, MyComponent);
Now let’s say I’m going to pass two collections into MyComponent
:
import { createContainer } from 'meteor/react-meteor-data';
import MyComponent from './MyComponent';
import { Orders } from '/imports/collections/orders';
import { Items } from '/imports/collections/items';
export default createContainer(() => {
/*
* Anything reactive (Mongo, Session, ReactiveVar, etc) in this function will
* cause it to re-run and pass props into MyComponent.
*/
const shopDataHandle = Meteor.subscribe('shopData');
const orders = Orders.find().fetch();
const items = Items.find().fetch();
return {
isDataReady: shopDataHandle.ready(),
items,
orders,
}
}, MyComponent);
We hit our first render, and all the data is displayed as expected. Now let’s say the Items
collection is updated. This will trigger a re-render. With createContainer
, you want to use MyComponent
's componentWillReceiveProps
lifecycle method to handle data updates.
class MyComponent extends Component {
componentWillReceiveProps(nextProps) {
if (this.props.items.length !== nextProps.items.length) {
this.setState({ itemCount: nextProps.items.length });
}
}
}
In this case, I’m imagining I have a count of items somewhere on the page, and so if that array changes, I’m updating the count.
So if you’re passing 2 or 3 collections into your component from createContainer
, and only one changes, componentWillReceiveProps
will always be executed no matter what. It’s up to you to figure out what changed and how to handle that.