Handle dynamically growing list in react js

Handle dynamically growing list in react js



I have a component for displaying a list, and this component is rendered from a parent component. I am currently generating the contents of this list using a for loop. Like -


let content =
for (let i = 0; i < list.length; i++)
content.push(
<div>
<p> list[i].something </p>
<p> list[i].somethingElse </p>
</div>
)


return content;



Now whenever a new object is added to this list, all the previous objects of the list, and the newly added object get rendered. This becomes extremely slow when the list contains around 1000 objects.



Is there a way by which only the new added can be added and rendered, without re-rendering all the previous entries of the list again?






Hi! Can you give us a more complete example of what you're doing with content? Please update your question with a Minimal, Complete, and Verifiable example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button). Stack Snippets support React, including JSX; here's how to do one.

– T.J. Crowder
Sep 9 '18 at 15:58


content


[<>]




2 Answers
2



This must be mainly because you havent added key, try the following code after setting an id for each list item and assign it to key prop.


key


let content =
for (let i = 0; i < list.length; i++)
content.push(
<div key=list[i].id>
<p> list[i].something </p>
<p> list[i].somethingElse </p>
</div>
)


return content;



If the list is a static one which doesnt change, index can also be used as the value for key prop.


let content =
for (let i = 0; i < list.length; i++)
content.push(
<div key=i>
<p> list[i].something </p>
<p> list[i].somethingElse </p>
</div>
)


return content;






In that case as I mentioned you can use the unique id for each item as the value for key prop instead of i.

– Rohith Murali
Sep 9 '18 at 15:59


i






If the list is a static list which doesnt change, using index is of no harm. And the reason i cant give key=id in the answer is that there is no specification about an available id in the question.

– Rohith Murali
Sep 9 '18 at 16:01


key=id


id






Something like the changed answer :) ?

– Rohith Murali
Sep 9 '18 at 16:06






Yeah, although I'd probably say "Have a unique id," not if you have a unique id, and show how to have one if they don't already (always-increasing number, whatever).

– T.J. Crowder
Sep 9 '18 at 16:07


id


id



You shoulda unique id for each item to render list item component. You can do on the ES6 something like:


const renderList = (list) => (
list.map(item =>
<div key=item.id>
<p>item.something</p>
<p>item.somethingElse</p>
</div>
);



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế