Mobx: How to handle pagination

Created on 22 Jun 2016  路  4Comments  路  Source: mobxjs/mobx

I have 10,000 movies in a db, which will be fetched in increments of 50 via actions. Given this store:

class MoviesStore {
@observable moviesList = [];

First time fetch populates moviesList with the first 50 movies via this.moviesList.replace(data); Second time fetch, should add the next 50 movies to moviesList but this.moviesList.push(data); doesn't work; however replacing works. Any idea how to get it to work as intended so it adds the second 50 movies to the first 50 movies in the array?

Most helpful comment

Note that you should use this.movieList.push(...data), push does not accept arrays (well, it will be added as single entry).

All 4 comments

but this.moviesList.push(data); doesn't work;

Do you mean that the change is not observed?

You can't use replace on arrays. You need to push each entry in data separately:

import {observable, autorun, transaction } from 'mobx';

const movieStore = observable({
  movieList: [1,2,3],
  displayList: function() {
    return this.movieList.join(', ');
  }
});

autorun(function() {
  console.log('movieList was changed', movieStore.displayList);
});

const data = [10, 11, 12];

transaction(function() {
  data.forEach(function(d) {
    movieStore.movieList.push(d);
  });
});

JS Bin

Note that you should use this.movieList.push(...data), push does not accept arrays (well, it will be added as single entry).

Thank you all! @mweststrate ...data worked, I was only 3 dots away haha ;-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mweststrate picture mweststrate  路  61Comments

fibric picture fibric  路  37Comments

davidmoshal picture davidmoshal  路  30Comments

AriaFallah picture AriaFallah  路  63Comments

Keats picture Keats  路  45Comments