user3527318
user3527318

Reputation: 143

Should I download all the data to redux or request them as needed?

I am writing my first React Redux ecommerce application and I wondering what is best practice, should I download all the data at ones or use server side pagination. When the user goes into the route "/products" I check to see if the state has the products if it does not then I fetch them.

componentDidMount(){
    if(this.props.products.length <= 0){
        var token = this.props.auth.token;
        this.props.actions.getProducts(token);
    }
}

But I am not sure if I should download all the products (1,000) and save them in the store or request them as needed. Example: I download 100 products and get all the pages and if the user wants to see the next 100 I fetch the next 100 products from server.

I will also be using this products in other parts of other forms.

What do you think?

Upvotes: 2

Views: 182

Answers (2)

Mike Tembo
Mike Tembo

Reputation: 9

use a server side pagination and high order component to handle getting the data from the server. and check relay and graphql they make you rethink data in your app. example check repo below good implimentation 1. https://github.com/reactjs/redux/tree/master/examples/real-world

4.https://github.com/emmenko/redux-react-router-async-example

Upvotes: 0

Hugo Mota
Hugo Mota

Reputation: 11567

Those are both valid methods. The "correct" one for your case will depend mostly on the type of information being retrieved.

For example, if it is a small list of small pieces of information, you may want to speed things up by retrieving everything at once.

However, if you are loading a potentially long list or the information is "heavy" (lot of fields or a big text), you probably will not want the client resources to be all used up like this. Nobody likes to see their network and memory swallowed because of one nasty page.

I don't know your data but a thousand products seems a bit much. My advice is: measure the size of the request and decide if it's worth to load everything at once or not.

Upvotes: 3

Related Questions