You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But make sure to render the parsed adjacent elements under a parent element since the parsed output is an array:
77
+
Since adjacent elements are parsed as an array, make sure to render them under a parent node:
78
78
79
79
```jsx
80
-
importReactfrom'react';
81
-
importparsefrom'html-react-parser';
82
-
83
-
functionApp() {
84
-
return (
85
-
<ul>
86
-
{parse(`
87
-
<li>Item 1</li>
88
-
<li>Item 2</li>
89
-
`)}
90
-
</ul>
91
-
);
92
-
}
80
+
<ul>
81
+
{parse(`
82
+
<li>Item 1</li>
83
+
<li>Item 2</li>
84
+
`)}
85
+
</ul>
93
86
```
94
87
95
88
Parse nested elements:
@@ -110,23 +103,43 @@ parse(
110
103
111
104
#### replace(domNode)
112
105
113
-
The `replace` method allows you to swap an element with your own React element.
106
+
The `replace` callback allows you to swap an element with another React element.
107
+
108
+
The first argument is an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2)'s [domhandler](https://github.com/fb55/domhandler#example):
109
+
110
+
```js
111
+
parse('<br>', {
112
+
replace:function(domNode) {
113
+
console.dir(domNode, { depth:null });
114
+
}
115
+
});
116
+
```
117
+
118
+
Console output:
114
119
115
-
The first argument is `domNode`―an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2)'s [domhandler](https://github.com/fb55/domhandler#example).
120
+
```js
121
+
{ type:'tag',
122
+
name:'br',
123
+
attribs: {},
124
+
children: [],
125
+
next:null,
126
+
prev:null,
127
+
parent:null }
128
+
```
116
129
117
-
The element is replaced only if a valid React element is returned.
130
+
The element is replaced only if a _valid_ React element is returned:
118
131
119
132
```js
120
133
parse('<p id="replace">text</p>', {
121
-
replace:function(domNode) {
134
+
replace:domNode=> {
122
135
if (domNode.attribs&&domNode.attribs.id==='replace') {
0 commit comments