Docs
Internals
mapArray()
⚠️

This function is part of the internal API. You should only be using this if you are making your own framework.


mapArray()

Syntax: mapArray(Block[])
Example: mapArray([block, block, block])

The mapArray function is used to create a Block list. It's the best way to render a view that's derived from array-like data. As the array changes, mapArray() updates or moves items in the DOM rather than recreating them. Let's look at an example:

import { block, patch, mapArray } from 'million';
 
const oldList = [1, 2, 3];
const newList = [3, 2, 1];
const list = block(({ item }) => {
  return <div>{item}</div>;
});
 
// updates list efficiently (only 2 moves instead of 3 updates)
patch(
  document.body,
  mapArray(oldList.map((item) => list({ item }))),
  mapArray(newList.map((item) => list({ item }))),
);