From 6a2d6b2e6e01a07d75bd1725191984ba37819266 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 6 Jul 2017 10:02:25 +0100 Subject: [PATCH] Fix a React duplicate key error If a single message contains the same link twice, we get an error from React about the clashing keys. De-dup the links to keep it quiet. --- src/components/views/messages/TextualBody.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/components/views/messages/TextualBody.js b/src/components/views/messages/TextualBody.js index 190b1341c3..209fba30aa 100644 --- a/src/components/views/messages/TextualBody.js +++ b/src/components/views/messages/TextualBody.js @@ -143,9 +143,15 @@ module.exports = React.createClass({ if (this.props.showUrlPreview && !this.state.links.length) { var links = this.findLinks(this.refs.content.children); if (links.length) { - this.setState({ links: links.map((link)=>{ - return link.getAttribute("href"); - })}); + // de-dup the links + const seen = new Set(); + links = links.filter((link) => { + if (seen.has(link)) return false; + seen.add(link); + return true; + }); + + this.setState({ links: links }); // lazy-load the hidden state of the preview widget from localstorage if (global.localStorage) { @@ -158,12 +164,13 @@ module.exports = React.createClass({ findLinks: function(nodes) { var links = []; + for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; if (node.tagName === "A" && node.getAttribute("href")) { if (this.isLinkPreviewable(node)) { - links.push(node); + links.push(node.getAttribute("href")); } } else if (node.tagName === "PRE" || node.tagName === "CODE" ||