V
V
vadimparinov2022-02-28 17:05:56
JavaScript
vadimparinov, 2022-02-28 17:05:56

How to implement sorting in React js?

Good afternoon! You need to implement the sorting of goods as in the layout (according to different parameters) I don’t quite understand how to do this yet. Can you suggest a direction where to dig?621cd6c07258e059440533.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wifftees, 2022-02-28
@vadimparinov

Implement an array of goods, each of which will be an object, for each write down what criteria it meets. For example like this:

{
  profession: false,
  videoCourse: false,
  any: true,
}

Further, when rendering the component, simply run filtering on all products (the main thing is to put all the logic in a separate hook)

M
marquis_de_eL1fe, 2022-02-28
@marquis_de_eL1fe

As wifftees already wrote , it is possible to implement each product as an object with criteria. Then, when choosing a specific sorting in the filters, it will only be necessary to search for products with criteria that match the filter. For example, you have "Video course" selected in the filter, so you only need products with a conditional

{
...
category: 'video_course',
...
}

inside the object.
We write a sorting function that takes your entire array of objects as input, using, for example, Array.filter() filter by the presence of the desired category of the category in the object, in our case it is 'video_course', and give back the finished filtered array.
UPD. Speaking specifically about React, we simply store the data that we will show in the state created using useState () and when choosing a filter, we simply update our state, as a result of which the displayed objects also change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question