I’ve put the reactjs:react
mixin into a setState
infinite loop in certain edge cases. this.data
eliminates that.
Using this.state
may seem like a great idea but ideally you want your data coming from props. You should avoid this.state
whenever possible (many times it’s not).
The best pattern with Meteor so far is to use the container pattern. Your FeedListContainer
handles the subscription and mixin, sideloading the data from Meteor. It passes this.data
into it’s only component FeedList
as props. The container only has one job, gather data and push it down via props.
The FeedList
component now knows nothing about how it’s getting it’s data. It’s basically a pure function that has params (props). It’s now very re-useable and easy to test (it just takes in props). FeedList
can implement shouldComponentUpdate
to cancel rendering on children.
FeedListContainer = React.createClass({
mixins: [ReactMeteorData],
getMeteorData: function() {
var sub = Meteor.subscribe("feed");
return {
feedReady: sub.ready(),
postItems: Posts.find({}, {sort: {createdAt: -1}}).fetch(),
};
},
render() {
return (
<FeedList
isReady={this.data.feedReady},
postItems={this.data.postItems}
/>
);
}
});
and then the feed list…
FeedList = React.createClass({
propTypes: {
postItems: React.PropTypes.object,
isReady: React.PropTypes.bool,
},
render() {
if (!this.props.isReady) {
return <LoadingSpinner />
}
return (
<div className='FeedList-container'>
{
this.props.map(function(post) {
return <Post postData={post} />
})
}
</div>
);
}
});
[quote="jedwards, post:64, topic:6150"]
How exactly do you mean about server-side data?
[/quote]
I think @sashko was referring to Flux users who are loading serverside data are not using this.state
in their component. Instead the component merely triggers an action (say and action called PRESSED_LOAD_MORE
) and loads in data from the server. This data is stored in a Flux store (global state for a domain). The component just requests state from the Flux store on render and never touches setState
(you could use setState to load the Flux data but you don’t have to).
this.state
should really only (ideally) be used to keep track of transient state of a component… say which tab is open in a tab list, or what photo a gallery is indexed at… not the feed of post data. You can do this but it doesn’t scale well.