SortedMap & SortedRenderMap
Last updated
Was this helpful?
Last updated
Was this helpful?
SortedMap
and SortedRenderMap
are key-based data structures. They are designed to store and sort the data inside of them by the position that is defined by a developer or generated automatically.
We wanted a data structure that will be flexible and works well with our . More specifically we wanted it to:
Sort content by the position
that is assigned to each element manually or automatically,
Give the ability to retrieve any element from the list by some unique key,
Be easily extensible with our plugin mechanism,
Be convenient and easy to use.
Both SortedMap
and SortedRenderMap
behave similarly, but the SortedRenderMap is a preferred way of storing React Components.
A new instance is declared by using createSortedMap()
or createSortedRenderMap()
functions from the @scandipwa/framework
package:
In this code snippet, we plug into the sortedMap
property to add a new item to the sorted map. The addition of new items is done via the addItem()
method that is present on all instances of SortedMap
and SortedRenderMap
. This method accepts 3 arguments:
item
, which is the item that you want to add to the list.
key
, which is the unique identifier for the newly added item.
position
, which is optional and specifies the position that the item should be inserted at.
The SECOND_ITEM_POSITION
variable was used to define the position of the new item. Its value is 1500
because of the positions that were assigned to the initial declaration: the first item is always assigned a position of 1000
and it increments by another 1000
for each item. So, in our example that would be:
In order to add an item in-between two items, a position value has to be between the position values of the surrounding items as well. I chose 1500
just for convenience, but it can be any number between 1001 and 1999 in this case.
The process of adding a new item is very straightforward. Just make sure that:
The key is unique to that declaration of SortedMap
/SortedRenderMap
The member argument is returned at the end
In order to remove an item from the SortedMap, you will need the key of that item and the removal process looks like this:
In order to get an item from the SortedMap, you will need the key of that item and the process looks like this:
At this point, the only thing that's left is to sort the data inside the map and retrieve it! With SortedRenderMap we can even get the array of React Components that are ready to be rendered:
You would want to extend SortedMap
and SortedRenderMap
for various reasons:
Rendering a new HTML block inside a React Component. For example, to add product recommendations on the product page
Adding a new React Context to the root component. For example, to make shop data available to every component in the app
Defining a new route for the app. For example, to add the login page under the route /login
.
Learn how to extend SortedMap
and SortedRenderMap
with these examples taken directly from the code.
Both functions accept a single argument that is an object containing the data. As you might notice, we don't pass the position
argument in any of the items and this is because the position is assigned automatically for items in the initial declaration. Now let's fill in the gap between one and three using the .