Skip to content
Advertisement

Why is getServerSideProps data being changed?

I’ll fetch data from an API, When I pass my getServerSideProps data to index.js, the prop array is in order by rank. [1,2,3..etc].

ex: data

JavaScript

When I alter that data into a different variable, for example:

JavaScript

console log shows data is now sorted by price too, when I only wanted topPrice to be sorted, why?

Advertisement

Answer

sort function alters your original list data. To avoid that case, you should create a copy of your list before calling sort

JavaScript

If you want it clearer, you can introduce a new variable to keep the new array

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement