How to force React component re-creation with a unique key

💡React Tip💡 Do you know that, you can pass a key to a component and passing a different key to component causes component re-creation? For example, <BookForm key={book.id} book={book} handleSubmit={handleSubmit} /> Whenever we pass a different key to the component, the component will be re-created again and all its data and state will be reset. This is really useful If you don’t want to retain the previous information / reset the component state, after some action. For example, on a single page, you’re opening different modals to display different information, then you can pass a unique key for each modal, so every time you open the modal, you will not have previous information retained and you will be able to display the correct respective modal data. So that’s one of the reasons you need a unique key that will not change during re-render while using the array map method. Because whenever the key changes, React re-creates that element and all its child elements including components you have nested inside that parent element, causing major performance issues. 𝗣𝗦: 𝗠𝗮𝘀𝘀𝗶𝘃𝗲 𝗱𝗶𝘀𝗰𝗼𝘂𝗻𝘁 𝗼𝗳𝗳𝗲𝗿 𝗼𝗳 𝟵𝟯% 𝗼𝗻 𝘁𝗵𝗲 𝗣𝗿𝗼 / 𝗟𝗶𝗳𝗲𝘁𝗶𝗺𝗲 𝗦𝘂𝗯𝘀𝗰𝗿𝗶𝗽𝘁𝗶𝗼𝗻 𝗮𝘀 𝗮 𝗽𝗮𝗿𝘁 𝗼𝗳 𝗗𝗶𝘄𝗮𝗹𝗶 + 𝗕𝗹𝗮𝗰𝗸 𝗳𝗿𝗶𝗱𝗮𝘆 𝘀𝗮𝗹𝗲 𝗶𝘀 𝗲𝗻𝗱𝗶𝗻𝗴 𝘀𝗼𝗼𝗻.. 𝗛𝘂𝗿𝗿𝘆 𝘂𝗽! 𝗟𝗶𝗻𝗸 𝗶𝗻 𝘁𝗵𝗲 𝗰𝗼𝗺𝗺𝗲𝗻𝘁 𝗮𝗻𝗱 𝗶𝗻 𝘁𝗵𝗲 𝗳𝗲𝗮𝘁𝘂𝗿𝗲𝗱 𝘀𝗲𝗰𝘁𝗶𝗼𝗻 𝗼𝗳 𝗺𝘆 𝗟𝗶𝗻𝗸𝗲𝗱𝗜𝗻 𝗽𝗿𝗼𝗳𝗶𝗹𝗲. #javascript #reactjs #nextjs #webdevelopment

To view or add a comment, sign in

Explore content categories