So I've been playing with ReactJS as a front end to Meteor, and unless I'm missing something, I'm not sure how I can use a reactive-var to update my news feed. Below is the code I have now:
Dashboard = React.createClass({
mixins: [ ReactMeteorData ],
getMeteorData() {
let subscription = Meteor.subscribe( 'newsFeed' );
let page = new ReactiveVar();
page.set(0);
return {
isLoading: !subscription.ready(),
news: News.find({}, {limit: 3, skip: (page.get() * 3), sort: {createdDate: 1}}).fetch(),
page: page,
totalCount: Counts.get( 'newsCounter' )
};
},
componentDidMount() {
},
newer() {
this.data.page.set(this.data.page.get() + 1);
console.log(this.data.page.get());
},
older() {
if(this.data.page.get() > 0)
this.data.page.set(this.data.page.get() - 1);
console.log(this.data.page.get());
},
render() {
if ( this.data.isLoading ) {
return <Loading />;
} else {
return (
<div>
<NewsList news={this.data.news} />
<ul className="pager">
<li className={this.data.page.get() <= 0 ? 'previous disabled' : 'previous' } onClick={this.older}><a href="#">← Older</a></li>
<li className="next" onClick={this.newer}><a href="#">Newer →</a></li>
</ul>
</div>
);
}
}
});
Currently when I click the newer button it does seem to increment the page variable but it never goes past 1, and the older button seemingly will take it from 1 back to 0, but I'm not 100% sure. I'm not sure what I'm getting wrong with the reactive variable, I know it doesn't work the same as Blaze which is obvious, but I can't seem to find any explanation or example of how to use them.
If someone could help me out, either through example, correction, or just a link to a decent piece of documentation that would be amazing.
You need to move your page variable into getInitialState.
Dashboard = React.createClass({
mixins: [ ReactMeteorData ],
getInitialState() {
let page = new ReactiveVar(0);
return { page };
},
getMeteorData() {
let subscription = Meteor.subscribe( 'newsFeed' );
return {
isLoading: !subscription.ready(),
news: News.find({}, {limit: 3, skip: (page.get() * 3), sort: {createdDate: 1}}).fetch(),
totalCount: Counts.get( 'newsCounter' )
};
},
componentDidMount() {
},
newer() {
this.state.page.set(this.state.page.get() + 1);
console.log(this.state.page.get());
},
older() {
if(this.state.page.get() > 0)
this.state.page.set(this.state.page.get() - 1);
},
render() {
if ( this.data.isLoading ) {
return <Loading />;
} else {
return (
<div>
<NewsList news={this.data.news} />
<ul className="pager">
<li className={this.state.page.get() <= 0 ? 'previous disabled' : 'previous' } onClick={this.older}><a href="#">← Older</a></li>
<li className="next" onClick={this.newer}><a href="#">Newer →</a></li>
</ul>
</div>
);
}
}
});