Clément CREUSAT
Clément CREUSAT

Reputation: 321

Always keep selected data in new Array (React Native)

I need help because I'm losing my mind haha ...

I have the main array products with this (it's just a sample) :

[
    {
        "from": "country",
        "maker": "name of maker",
        "id": "1969",
        "image": "image.jpg",
        "label": "355",
        "name": "name of product",
        "price": "12.90",
        "subscriber_price": "8.90",
        "url_path": "url",
        "occasion": null,
        "colour": "31",
        "origin": "397",
    },
    {
        "from": "country",
        "maker": "name of maker",
        "id": "2043",
        "image": "image.jpg",
        "label": "362",
        "name": "name of product",
        "price": "24.90",
        "subscriber_price": "24.90",
        "url_path": "url",
        "occasion": "51,376,155,39",
        "colour": "31",
        "origin": "395"
    }
]

I'm working this the Picker Component. So, what I'm doing is : I have a Picker to select products with their "colour".. then I have another one to filter the selected products (only with colour:31 for example) with their "origin" and finally I want to filter them through their "label" ...

The fact is I have 3 Pickers, 3 functions to select them and it's working but the problem is I'm erasing with a setState my render of "displayProducts". So, when I have selected the 3 options, I can't go back..

For example, I choose "colour:31" with "origin:397" and "label:355" .. I can't go back and tell : finally I want "origin:395" because it doesn't exist anymore, etc... and one "colour" can have different "label, origin, ..."

I'm doing something like this but it's only available for ONE option and not multiple options and without keeping a solution to find again my filtered products :

onChangeGetOrigin(originValue) {
    this.setState(() => ({
        activeOrigin: originValue,
        displayProducts: this.state.displayProducts.filter(product => product.origin == originValue)
    }));
}

Do anyone can understand what I'm saying ? :-D

Upvotes: 0

Views: 237

Answers (1)

Saurav
Saurav

Reputation: 61

You can maintain two arrays. One contains the complete list of products and the other one is a derived array after applying the filters. You can use the derived list for display and original array for selection.

Upvotes: 1

Related Questions