I have a note taking app which allows users to add notes. I am trying to render newly created notes on the home page, but when I go to the add note and add a note, then go back to the home page, it shows the new notes(limited to 10) but the new note that was just created is at the bottom of the list, instead of the top. When I refresh the page, it goes to the top like I want it to. How would I get it so that I don't have to refresh the page to get the newly created note at the top?
I try to sort by using a timestamp, (using Date.parse(new Date())
), and then publishing from server using this,
Meteor.publish('notes-newest', function () {
return Notes.find({},{sort: {createdAt: -1}, limit: 10});
});
Here is where I am trying to render it:
componentDidMount() {
Meteor.subscribe('notes-newest')
this.tracker = Tracker.autorun(() => {
const notes = Notes.find().fetch()
if(notes == null || notes == undefined){
return;
}
this.setState({ notes });
console.log(notes);
})
}