Skip to content

Instantly share code, notes, and snippets.

@YousraBD
Last active October 24, 2021 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YousraBD/45adac445c5b0abb827f2ed8ba42944e to your computer and use it in GitHub Desktop.
Save YousraBD/45adac445c5b0abb827f2ed8ba42944e to your computer and use it in GitHub Desktop.
<List
itemKey={faker.datatype.uuid}
itemData={data}
innerElementType="ul"
itemCount={data.length}
itemSize={20}
height={700}
width={400}
>
{({ data, index, style }) => {
return <li style={style}>{data[index]}</li>;
}}
</List>
$ yarn add faker
import { FixedSizeList as List } from "react-window";
const App = () => {
const [data, setData] = useState(() =>
Array.from({ length: 10000 }, faker.address.city)
);
const reverse = () => {
setData((data) => data.slice().reverse());
};
return (
<main>
<button onClick={reverse}>Reverse</button>
<List
innerElementType="ul"
itemCount={data.length}
itemSize={20}
height={700}
width={400}
>
{({ index, style }) => {
return (
<li style={style}>
{data[index]}
</li>
);
}}
</List>
</main>
);
};
import React, { useState } from "react";
import * as faker from "faker";
const App = () => {
const [data, setData] = useState(() =>
Array.from({ length: 10000 }, faker.address.city)
);
return (
<main>
<ul style={{ width: "400px", height: "700px", overflowY: "scroll" }}>
{data.map((city, i) => (
<li key={i + city}>{city}</li>
))}
</ul>
</main>
);
};
<List
useIsScrolling={true}
itemCount={data.length}
itemSize={20}
height={700}
width={400}
>
{({ index, style, isScrolling }) =>
isScrolling ? (
<Skeleton style={style} />
) : (
<ExpensiveItem index={index} style={style} />
)
}
</List>;
<List
itemData={data}
innerElementType="ul"
itemCount={data.length}
itemSize={20}
height={700}
width={400}
>
{({ data, index, style }) => {
return <li style={style}>{data[index]}</li>;
}}
</List>
$ yarn add react-window # the library
$ yarn add -D @types/react-window # auto-completion
const App = () => {
const [data, setData] = useState(() =>
Array.from({ length: 10000 }, faker.address.city)
);
const reverse = () => {
setData((data) => data.slice().reverse());
};
return (
<main>
<button onClick={reverse}>Reverse</button>
<ul style={{ width: "400px", height: "700px", overflowY: "scroll" }}>
{data.map((city, i) => (
<li style={{ height: "20px" }} key={i + city}>{city}</li>
))}
</ul>
</main>
);
};
import { FixedSizeGrid as Grid } from "react-window";
import * as faker from "faker";
const COLUMNS = 18;
const ROWS = 30;
const data = Array.from({ length: ROWS }, () =>
Array.from({ length: COLUMNS }, faker.internet.avatar)
);
function App() {
return (
<Grid
columnCount={COLUMNS}
rowCount={ROWS}
columnWidth={50}
rowHeight={50}
height={500}
width={600}
>
{({ rowIndex, columnIndex, style }) => {
return <img src={data\[rowIndex\][columnIndex]} alt="" />;
}}
</Grid>
);
}
@cassianodaniel
Copy link

Thanks for the example in datatype.uuid.js:

<List
itemKey={faker.datatype.uuid}
itemData={data}
innerElementType="ul"
itemCount={data.length}
itemSize={20}
height={700}
width={400}

{({ data, index, style }) => {
return

  • {data[index]}
  • ;
    }}

    I was having a hard time trying to destructure the list children props from

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment