I got this warning: VirtualizedList: You have a large list that is slow to update – make sure your renderItem function renders components that follow React performance best practices like PureComponent, shouldComponentUpdate, etc. {“contentLength”: 8832, “dt”: 1939, “prevDt”: 1122}
code 1
import React, { useMemo } from 'react';
const Verse = React.memo(({ coord, verse, color }) => (
<div key={coord.key} style={{position: 'absolute', top: `${coord.top}%`, right: `${coord.right}%`, width: `${coord.width}%`, height: `${coord.height}%`, backgroundColor: color}}>
{verse}
</div>
));
const renderItem = (coord, verse, color) => <Verse coord={coord} verse={verse} color={color} />;
const MushafPage = React.memo(({ pageData, parts = 2 }) => {
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'pink']; // Array of colors
// Calculate the total height of all verses
const totalHeight = useMemo(() => pageData.reduce((total, item) => total + item.coord.reduce((totalCoord, coord) => totalCoord + coord.top + coord.height, 0), 0), [pageData]);
// Calculate the height of each part
const partHeight = totalHeight / parts;
// Initialize the current part and its height
let currentPart = 0;
let currentHeight = 0;
return (
<div>
{pageData.map((item) => {
return item.coord.map((coord, index) => {
// Add the height of the current item to the current height
currentHeight += coord.top + coord.height;
// If the current height exceeds the part height, move to the next part
if (currentHeight > partHeight) {
currentPart++;
currentHeight -= partHeight; // Subtract the partHeight from the currentHeight
}
// Choose a color based on the current part
const color = colors[currentPart % colors.length];
return renderItem(coord, item.verse, color);
});
})}
</div>
);
});
export default MushafPage;
The code above will be imported into the this file code ,
code 2:
import React, { useEffect, useState } from 'react';
import MushafPage from '../../components/MushafPage';
// Import JSON files
import data1 from '../../assets/mushaf/juz30/content/1.json';
import data2 from '../../assets/mushaf/juz30/content/2.json';
import data3 from '../../assets/mushaf/juz30/content/3.json';
import data4 from '../../assets/mushaf/juz30/content/4.json';
function Block() {
const [pageData, setPageData] = useState([]);
useEffect(() => {
// Combine all JSON data into one array
const allData = [data1, data2, data3, data4];
setPageData(allData.flat());
}, []);
return (
<div className="Block">
{pageData.map((data, index) => <MushafPage key={index} pageData={data} parts={2} />)}
</div>
);
}
export default React.memo(Block);
then in it there are .json files which are the basis for dividing the pages, like this
code 3:
[
{
"key": "581-1",
"verse": "78:1",
"coord": [
{
"key": "78:1:1",
"top": 21.6,
"right": 2.2,
"width": 26.1,
"height": 4.7
}
]
},
{
"key": "581-2",
"verse": "78:2",
"coord": [
{
"key": "78:2:1",
"top": 21.6,
"right": 28.4,
"width": 28.1,
"height": 4.7
}
]
},
{
"key": "581-3",
"verse": "78:3",
"coord": [
{
"key": "78:3:1",
"top": 21.6,
"right": 56.75,
"width": 36.2,
"height": 4.7
}
]
},
{
"key": "581-4",
"verse": "78:4",
"coord": [
{
"key": "78:4:1",
"top": 26.7,
"right": 2.2,
"width": 26.1,
"height": 4.7
}
]
},
{
"key": "581-5",
"verse": "78:5",
"coord": [
{
"key": "78:5:1",
"top": 26.7,
"right": 28.75,
"width": 28.6,
"height": 4.7
}
]
},
{
"key": "581-6",
"verse": "78:6",
"coord": [
{
"key": "78:6:1",
"top": 26.7,
"right": 57.5,
"width": 36.1,
"height": 4.7
}
]
},
{
"key": "581-7",
"verse": "78:7",
"coord": [
{
"key": "78:7:1",
"top": 31.8,
"right": 2.2,
"width": 27.1,
"height": 4.7
}
]
},
{
"key": "581-8",
"verse": "78:8",
"coord": [
{
"key": "78:8:1",
"top": 31.8,
"right": 31,
"width": 30.1,
"height": 4.7
}
]
},
{
"key": "581-9",
"verse": "78:9",
"coord": [
{
"key": "78:9:1",
"top": 31.8,
"right": 61.75,
"width": 32,
"height": 4.7
},
{
"key": "78:9:2",
"top": 36.9,
"right": 2.2,
"width": 5.1,
"height": 4.7
}
]
},
{
"key": "581-10",
"verse": "78:10",
"coord": [
{
"key": "78:10:1",
"top": 36.9,
"right": 7.2,
"width": 35.6,
"height": 4.7
}
]
},
{
"key": "581-11",
"verse": "78:11",
"coord": [
{
"key": "78:11:1",
"top": 36.9,
"right": 42.9,
"width": 39.1,
"height": 4.7
}
]
},
{
"key": "581-12",
"verse": "78:12",
"coord": [
{
"key": "78:12:1",
"top": 36.9,
"right": 82.25,
"width": 11.5,
"height": 4.7
},
{
"key": "78:12:2",
"top": 42.05,
"right": 2.2,
"width": 36.6,
"height": 4.7
}
]
},
{
"key": "581-13",
"verse": "78:13",
"coord": [
{
"key": "78:13:1",
"top": 42.05,
"right": 40,
"width": 40.6,
"height": 4.7
}
]
},
{
"key": "581-14",
"verse": "78:14",
"coord": [
{
"key": "78:14:1",
"top": 42.05,
"right": 82,
"width": 11.5,
"height": 4.7
},
{
"key": "78:14:2",
"top": 47.15,
"right": 2.2,
"width": 40.1,
"height": 4.7
}
],
"covers": {
"firstWord": [
{
"key": "78:14:1",
"top": 47.15,
"right": 2.2,
"width": 35,
"height": 4.7
}
],
"invisible": [
{
"key": "78:14:1",
"top": 42.05,
"right": 82,
"width": 11.5,
"height": 4.7
},
{
"key": "78:14:2",
"top": 47.15,
"right": 2.2,
"width": 35,
"height": 4.7
}
]
}
},
{
"key": "581-15",
"verse": "78:15",
"coord": [
{
"key": "78:15:1",
"top": 47.15,
"right": 42.47,
"width": 38.1,
"height": 4.7
}
]
},
{
"key": "581-16",
"verse": "78:16",
"coord": [
{
"key": "78:16:1",
"top": 47.15,
"right": 81.5,
"width": 12,
"height": 4.7
},
{
"key": "78:16:2",
"top": 52.35,
"right": 2.2,
"width": 14.6,
"height": 4.7
}
]
},
{
"key": "581-17",
"verse": "78:17",
"coord": [
{
"key": "78:17:1",
"top": 52.35,
"right": 17.5,
"width": 45.1,
"height": 4.7
}
]
},
{
"key": "581-18",
"verse": "78:18",
"coord": [
{
"key": "78:18:1",
"top": 52.35,
"right": 62.75,
"width": 30,
"height": 4.7
},
{
"key": "78:18:2",
"top": 57.45,
"right": 2.2,
"width": 24.6,
"height": 4.7
}
]
},
{
"key": "581-19",
"verse": "78:19",
"coord": [
{
"key": "78:19:1",
"top": 57.45,
"right": 27.3,
"width": 50.1,
"height": 4.7
}
]
},
{
"key": "581-20",
"verse": "78:20",
"coord": [
{
"key": "78:20:1",
"top": 57.45,
"right": 77.75,
"width": 15.5,
"height": 4.7
},
{
"key": "78:20:2",
"top": 62.55,
"right": 2.2,
"width": 36.1,
"height": 4.7
}
]
},
{
"key": "581-21",
"verse": "78:21",
"coord": [
{
"key": "78:21:1",
"top": 62.55,
"right": 39,
"width": 39.85,
"height": 4.7
}
]
},
{
"key": "581-22",
"verse": "78:22",
"coord": [
{
"key": "78:22:1",
"top": 62.55,
"right": 78.85,
"width": 14.75,
"height": 4.7
},
{
"key": "78:22:2",
"top": 67.65,
"right": 2.2,
"width": 13.85,
"height": 4.7
}
]
},
{
"key": "581-23",
"verse": "78:23",
"coord": [
{
"key": "78:23:1",
"top": 67.7,
"right": 16.25,
"width": 33.6,
"height": 4.7
}
]
},
{
"key": "581-24",
"verse": "78:24",
"coord": [
{
"key": "78:24:1",
"top": 67.7,
"right": 50,
"width": 43.5,
"height": 4.7
},
{
"key": "78:24:2",
"top": 72.85,
"right": 2.2,
"width": 5.1,
"height": 4.7
}
] },
{
"key": "581-25",
"verse": "78:25",
"coord": [
{
"key": "78:25:1",
"top": 72.85,
"right": 7.4,
"width": 37.6,
"height": 4.7
}
]
},
{
"key": "581-26",
"verse": "78:26",
"coord": [
{
"key": "78:26:1",
"top": 72.85,
"right": 45,
"width": 27.6,
"height": 4.7
}
]
},
{
"key": "581-27",
"verse": "78:27",
"coord": [
{
"key": "78:27:1",
"top": 72.85,
"right": 73,
"width": 20,
"height": 4.7
},
{
"key": "78:27:2",
"top": 77.95,
"right": 2.2,
"width": 32.6,
"height": 4.7
}
]
},
{
"key": "581-28",
"verse": "78:28",
"coord": [
{
"key": "78:28:1",
"top": 77.95,
"right": 34.9,
"width": 43.1,
"height": 4.7
}
]
},
{
"key": "581-29",
"verse": "78:29",
"coord": [
{
"key": "78:29:1",
"top": 77.95,
"right": 78,
"width": 15.5,
"height": 4.7
},
{
"key": "78:29:2",
"top": 83.05,
"right": 2.2,
"width": 31.1,
"height": 4.7
}
],
"covers": {
"firstWord": [
{
"key": "78:29:1",
"top": 77.95,
"right": 85.25,
"width": 8.25,
"height": 4.7
},
{
"key": "78:29:2",
"top": 83.05,
"right": 2.2,
"width": 26,
"height": 4.7
}
]
},
{
"key": "581-30",
"verse": "78:30",
"coord": [
{
"key": "78:30:1",
"top": 83.05,
"right": 33.75,
"width": 59.1,
"height": 4.95
}
]
}
]
The context is that I want to divide a manuscript page into several parts (2-8 parts) using Code 1, which applies color. After that, I use Code 2 to import Code 1, enabling it to be applied to five .json files. These files contain coordinate image data for the start and end of each paragraph. Code 3 refers to the original .json file, which contains the coordinates of each item and the length of the paragraph. In the original implementation, there were ten .json files
I have tried many times to optimize my code but the program still gives the same warning, namely: VirtualizedList: You have a large list that is slow to update – make sure your renderItem function renders components that follow React performance best practices like PureComponent, shouldComponentUpdate, etc. {“contentLength”: 8832, “dt”: 1939, “prevDt”: 1122}