Block Lists

<For />

Syntax: <For each={array}>{(item, index) => Block}</For>
Example: <For each={[1, 2, 3]}>{(item) => myBlock({ item })}</For>

The <For /> component is used to render a list of blocks. It takes an array as the each prop and a function as its children. The function is called for each item in the array and is passed the item and its index as arguments.

It's the best way to loop over an array. As the array changes, <For /> updates or moves items in the DOM rather than recreating them. Let's look at an example:

import { createRoot } from 'react-dom/client';
import { block } from 'million/react';
function Display() {
  return <div>{this.props.item}</div>;
const DisplayBlock = block(Display);
function App() {
  const [items, setItems] = useState([1, 2, 3]);
  return (
      <button onClick={() => setItems([...items, items.length + 1])}>
        Add item
      <For each={items}>{(item) => <DisplayBlock item={item} />}</For>
createRoot(document.getElementById('root')).render(<App />);