0

i'm trying to add product filtering functionality like this: filter product image and after add filter i have to clear filter as user wish like this one: clear filter image if anyone give me real world idea with some more details or short word it can help me be more proffessional.

  • Add some code so we can help you. – Amirhossein Dec 03 '22 at 17:30
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 03 '22 at 17:30
  • @Amirhossein thank you for your response. i just update my problem. still if you not clear my think please stay with your valuable comment. – Ashraful Mijan Dec 04 '22 at 14:07

1 Answers1

0

Well let's assume your filters data is like this:

[
    {
        catrgory: "Color",
        values: [
            "Silver",
            "Black",
            ...
        ]
    },
    {
        catrgory: "Material",
        values: [
            "Acrylic",
            "Rayon",
            ...
        ]
    },
    ...
]
  1. You should have 2 states in your component. One for holding the filters data and another for holding the selected filters.

  2. Fetch your filters data from the server once. (Or use a local file).

  3. Each time the user selects a filter, you should add it to your selected filters data.

  4. Each time the user remove a filter, you should remove it from your selected filters data.

Hope it helps (It's just a guide not the whole solution):

const MyComponent = () => {
    const [filtersData, setFiltersData] = useState([]);
    const [selectedFilters, setSelectedFilters] = useState([]);

    useEffect(() => {
        // fetch data from the server
    }, []);

    const handleSelectFilter = (category, value) => {
        const newSelectedFilters = [...selectedFilters];
        let filter = newSelectedFilters.find((selectedFilter) => selectedFilter.category === category);
        if(filter) {
            filter.values.push(value);
        } else {
            filter = {
                catrgoty: category,
                values: [value]
            }
            newSelectedFilters.push(filter);
        }
        setSelectedFilters(newSelectedFilters);
    }

    const handleDeleteFilter = (category, value) => {
        let newSelectedFilters = [...selectedFilters];
        const index = newSelectedFilters.findIndex((selectedFilter) => selectedFilter.category === category);
        newSelectedFilters = newSelectedFilters.splice(index, 1);
        setSelectedFilters(newSelectedFilters);
    }

    return (
        <div>
            {
                filtersData.map((filterItem, index) => {
                return (
                    <div key={index}>
                    <div>{filterItem.category}</div>
                    {
                        filterItem.values.map((value) => {
                        return (
                            <div key={value} onClick={() => handleSelectFilter(filterItem.category, value)}>{value}</div>
                        )
                        })
                    }
                    </div>
                )
                })
            }
            {
                selectedFilters.map((selectedFilter, index) => {
                return (
                    <div key={index}>
                    <div>{selectedFilter.category}</div>
                    {
                        selectedFilter.values.map((value) => {
                        return (
                            <div key={value} onClick={() => handleDeleteFilter(filterItem.category, value)}>{value}</div>
                        )
                        })
                    }
                    </div>
                )
                })
            }
        </div>
    );
}
Amirhossein
  • 1,819
  • 1
  • 6
  • 10
  • thank you @Amirhossein i appriciate with your thinking. but this solution it's not enough for my real project. let me explain why not : **because after reload it reset selectedFilter State. if you think now then we can use localStorage still have problem with this one also because if i sent you browser url after adding filter you did't get with filter. for your more understanding [please visit](https://www.aliexpress.com/category/200003482/dresses.html?trafficChannel=main&catName=dresses&CatId=200003482&ltype=wholesale&SortType=default&page=1&pvid=14-350853%2C10-100019007&isrefine=y) – Ashraful Mijan Dec 04 '22 at 14:56
  • You're welcome. Oh now I understand. Why you don't just add each filter to the url and then with each page load, read them from the url and set your states ? – Amirhossein Dec 04 '22 at 15:03
  • Yes it is. Most websites like Amazon do that in this way. – Amirhossein Dec 04 '22 at 15:49