Skip to content

Commit 7522a0a

Browse files
Merge pull request #18 from remarkablemark/remove-replace-key
Remove `key` parameter from `replace` and use `React.cloneElement` instead
2 parents ddd289b + 26e4e7c commit 7522a0a

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

.eslintrc

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"indent": [
1313
"error",
1414
4,
15-
{ SwitchCase: 1 }
15+
{ "SwitchCase": 1 }
1616
],
1717
"linebreak-style": [
1818
"error",
@@ -26,8 +26,11 @@
2626
"error",
2727
"always"
2828
],
29+
"no-unused-vars": [
30+
"error",
31+
{ "vars": "all", "args": "none" }
32+
],
2933
"strict": 2,
30-
"no-unused-vars": 2,
3134
"no-cond-assign": 2,
3235
"camelcase": 1
3336
}

README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,16 @@ ReactDOM.render(
6868

6969
### Options
7070

71-
#### replace(domNode, key)
71+
#### replace(domNode)
7272

7373
The `replace` method allows you to swap an element with your own React element.
7474

75-
The method has 2 parameters:
75+
The method has 1 parameter:
7676
1. `domNode`: An object which shares the same schema as the output from [htmlparser2.parseDOM](https://github.com/fb55/domhandler#example).
77-
2. `key`: A number to keep track of the element. You should set it as the ["key" prop](https://fb.me/react-warning-keys) in case your element has siblings.
7877

7978
```js
8079
Parser('<p id="replace">text</p>', {
81-
replace: function(domNode, key) {
80+
replace: function(domNode) {
8281
console.log(domNode);
8382
// { type: 'tag',
8483
// name: 'p',
@@ -88,8 +87,6 @@ Parser('<p id="replace">text</p>', {
8887
// prev: null,
8988
// parent: null }
9089

91-
console.log(key); // 0
92-
9390
return;
9491
// element is not replaced because
9592
// a valid React element is not returned
@@ -106,14 +103,13 @@ var React = require('react');
106103
var html = '<div><p id="main">replace me</p></div>';
107104

108105
var reactElement = Parser(html, {
109-
replace: function(domNode, key) {
106+
replace: function(domNode) {
110107
if (domNode.attribs && domNode.attribs.id === 'main') {
111108
return React.createElement('span', {
112-
key: key,
113109
style: { fontSize: '42px' } },
114110
'replaced!');
115111
// equivalent jsx syntax:
116-
// return <span key={key} style={{ fontSize: '42px' }}>replaced!</span>;
112+
// return <span style={{ fontSize: '42px' }}>replaced!</span>;
117113
}
118114
}
119115
});

lib/dom-to-react.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ function domToReact(nodes, options) {
2828

2929
// replace with custom React element (if applicable)
3030
if (isReplacePresent) {
31-
replacement = options.replace(node, i); // i = key
31+
replacement = options.replace(node);
3232

3333
if (React.isValidElement(replacement)) {
34+
// specify a "key" prop if element has siblings
35+
// https://fb.me/react-warning-keys
36+
if (len > 1) {
37+
replacement = React.cloneElement(replacement, { key: i });
38+
}
3439
result.push(replacement);
3540
continue;
3641
}

test/html-to-react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ describe('html-to-react', function() {
7777
it('overrides the element if replace is valid', function() {
7878
var html = data.html.complex;
7979
var reactElement = Parser(html, {
80-
replace: function(node, key) {
80+
replace: function(node) {
8181
if (node.name === 'title') {
82-
return React.createElement('title', { key: key }, 'Replaced Title');
82+
return React.createElement('title', {}, 'Replaced Title');
8383
}
8484
}
8585
});

0 commit comments

Comments
 (0)