Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eradicate the 'unique "key" prop' warnings #8747

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions website/core/Marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var React = require('React');
var Prism = require('Prism');
var WebPlayer = require('WebPlayer');
var Header = require('Header');
var sequentialKey = require('sequentialKey');

/**
* Block-Level Grammar
Expand Down Expand Up @@ -580,7 +581,7 @@ InlineLexer.prototype.output = function(src) {
text = cap[1];
href = text;
}
out.push(React.DOM.a({href: this.sanitizeUrl(href)}, text));
out.push(React.DOM.a({key: sequentialKey(), href: this.sanitizeUrl(href)}, text));
continue;
}

Expand All @@ -589,7 +590,7 @@ InlineLexer.prototype.output = function(src) {
src = src.substring(cap[0].length);
text = cap[1];
href = text;
out.push(React.DOM.a({href: this.sanitizeUrl(href)}, text));
out.push(React.DOM.a({key: sequentialKey(), href: this.sanitizeUrl(href)}, text));
continue;
}

Expand All @@ -599,7 +600,9 @@ InlineLexer.prototype.output = function(src) {

var color = cap[0].match('<color ([^ ]+) />');
if (color) {
out.push(React.DOM.span({className: 'color', style: {backgroundColor: color[1]}}));
out.push(React.DOM.span(
{key: sequentialKey(), className: 'color', style: {backgroundColor: color[1]}}
));
continue;
}

Expand Down Expand Up @@ -636,35 +639,35 @@ InlineLexer.prototype.output = function(src) {
// strong
if (cap = this.rules.strong.exec(src)) {
src = src.substring(cap[0].length);
out.push(React.DOM.strong(null, this.output(cap[2] || cap[1])));
out.push(React.DOM.strong({key: sequentialKey()}, this.output(cap[2] || cap[1])));
continue;
}

// em
if (cap = this.rules.em.exec(src)) {
src = src.substring(cap[0].length);
out.push(React.DOM.em(null, this.output(cap[2] || cap[1])));
out.push(React.DOM.em({key: sequentialKey()}, this.output(cap[2] || cap[1])));
continue;
}

// code
if (cap = this.rules.code.exec(src)) {
src = src.substring(cap[0].length);
out.push(React.DOM.code(null, cap[2]));
out.push(React.DOM.code({key: sequentialKey()}, cap[2]));
continue;
}

// br
if (cap = this.rules.br.exec(src)) {
src = src.substring(cap[0].length);
out.push(React.DOM.br(null, null));
out.push(React.DOM.br({key: sequentialKey()}, null));
continue;
}

// del (gfm)
if (cap = this.rules.del.exec(src)) {
src = src.substring(cap[0].length);
out.push(React.DOM.del(null, this.output(cap[1])));
out.push(React.DOM.del({key: sequentialKey()}, this.output(cap[1])));
continue;
}

Expand Down Expand Up @@ -715,12 +718,14 @@ InlineLexer.prototype.outputLink = function(cap, link) {
&& link.href.charAt(0) !== '#';

return React.DOM.a({
key: sequentialKey(),
href: this.sanitizeUrl(link.href),
title: link.title,
target: shouldOpenInNewWindow ? '_blank' : ''
}, this.output(cap[1]));
} else {
return React.DOM.img({
key: sequentialKey(),
src: this.sanitizeUrl(link.href),
alt: cap[1],
title: link.title
Expand Down Expand Up @@ -816,11 +821,12 @@ Parser.prototype.tok = function() {
return [];
}
case 'hr': {
return React.DOM.hr(null, null);
return React.DOM.hr({key: sequentialKey()}, null);
}
case 'heading': {
return (
<Header
key={sequentialKey()}
level={this.token.depth}
toSlug={this.token.text}>
{this.inline.output(this.token.text)}
Expand All @@ -833,11 +839,11 @@ Parser.prototype.tok = function() {

if (lang && lang.indexOf('ReactNativeWebPlayer') === 0) {
return (
<WebPlayer params={lang.split('?')[1]}>{text}</WebPlayer>
<WebPlayer key={sequentialKey()} params={lang.split('?')[1]}>{text}</WebPlayer>
);
}

return <Prism>{text}</Prism>;
return <Prism key={sequentialKey()}>{text}</Prism>;
}
case 'table': {
var table = []
Expand All @@ -853,12 +859,12 @@ Parser.prototype.tok = function() {
heading = this.inline.output(this.token.header[i]);
row.push(React.DOM.th(
this.token.align[i]
? {style: {textAlign: this.token.align[i]}}
? {key: sequentialKey(), style: {textAlign: this.token.align[i]}}
: null,
heading
));
}
table.push(React.DOM.thead(null, React.DOM.tr(null, row)));
table.push(React.DOM.thead({key: sequentialKey()}, React.DOM.tr(null, row)));

// body
for (i = 0; i < this.token.cells.length; i++) {
Expand All @@ -867,16 +873,16 @@ Parser.prototype.tok = function() {
for (j = 0; j < cells.length; j++) {
row.push(React.DOM.td(
this.token.align[j]
? {style: {textAlign: this.token.align[j]}}
? {key: sequentialKey(), style: {textAlign: this.token.align[j]}}
: null,
this.inline.output(cells[j])
));
}
body.push(React.DOM.tr(null, row));
body.push(React.DOM.tr({key: sequentialKey()}, row));
}
table.push(React.DOM.thead(null, body));
table.push(React.DOM.thead({key: sequentialKey()}, body));

return React.DOM.table(null, table);
return React.DOM.table({key: sequentialKey()}, table);
}
case 'blockquote_start': {
var body = [];
Expand All @@ -885,7 +891,7 @@ Parser.prototype.tok = function() {
body.push(this.tok());
}

return React.DOM.blockquote(null, body);
return React.DOM.blockquote({key: sequentialKey()}, body);
}
case 'list_start': {
var type = this.token.ordered ? 'ol' : 'ul'
Expand All @@ -895,7 +901,7 @@ Parser.prototype.tok = function() {
body.push(this.tok());
}

return React.DOM[type](null, body);
return React.DOM[type]({key: sequentialKey()}, body);
}
case 'list_item_start': {
var body = [];
Expand All @@ -906,7 +912,7 @@ Parser.prototype.tok = function() {
: this.tok());
}

return React.DOM.li(null, body);
return React.DOM.li({key: sequentialKey()}, body);
}
case 'loose_item_start': {
var body = [];
Expand All @@ -915,22 +921,22 @@ Parser.prototype.tok = function() {
body.push(this.tok());
}

return React.DOM.li(null, body);
return React.DOM.li({key: sequentialKey()}, body);
}
case 'html': {
return !this.token.pre && !this.options.pedantic
? React.DOM.span({dangerouslySetInnerHTML: {__html: this.token.text}})
? React.DOM.span({key: sequentialKey(), dangerouslySetInnerHTML: {__html: this.token.text}})
: this.token.text;
}
case 'paragraph': {
return this.options.paragraphFn
? this.options.paragraphFn.call(null, this.inline.output(this.token.text))
: React.DOM.p(null, this.inline.output(this.token.text));
: React.DOM.p({key: sequentialKey()}, this.inline.output(this.token.text));
}
case 'text': {
return this.options.paragraphFn
? this.options.paragraphFn.call(null, this.parseText())
: React.DOM.p(null, this.parseText());
: React.DOM.p({key: sequentialKey()}, this.parseText());
}
}
};
Expand Down Expand Up @@ -1056,8 +1062,8 @@ function marked(src, opt, callback) {
} catch (e) {
e.message += '\nPlease report this to https://github.com/chjj/marked.';
if ((opt || marked.defaults).silent) {
return [React.DOM.p(null, "An error occurred:"),
React.DOM.pre(null, e.message)];
return [React.DOM.p({key: sequentialKey()}, "An error occurred:"),
React.DOM.pre({key: sequentialKey()}, e.message)];
}
throw e;
}
Expand Down Expand Up @@ -1105,7 +1111,7 @@ marked.parse = marked;
var Marked = React.createClass({
render: function() {
return this.props.children ?
React.DOM.div(null, marked(this.props.children, this.props)) :
React.DOM.div({key: sequentialKey()}, marked(this.props.children, this.props)) :
null;
}
});
Expand Down
23 changes: 23 additions & 0 deletions website/core/sequentialKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule sequentialKey
*/

// Create a sequential key for rendered DOM elements to avoid:
// "Each child in an array or iterator should have a unique "key" prop.""
// warnings. Given that the DOM for the docs will not have any state changes so
// the counter will always be the same and determinisic for any component.

var keyCounter = 0;

function sequentialKey() {
return keyCounter++;
}

module.exports = sequentialKey;
Loading