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
The parser converts an HTML string to [React element(s)](https://facebook.github.io/react/docs/glossary.html#react-elements). You can also `replace` element(s) with your own custom React element(s) via the parser options.
16
+
The parser converts a string of HTML to [React Element(s)](https://facebook.github.io/react/docs/glossary.html#react-elements).
17
17
18
-
### Example
18
+
There is also an option to [replace](#replacedomnode) element(s) with your own React Element(s) via the [parser options](#options).
19
+
20
+
#### Example
19
21
20
22
```js
21
23
var Parser =require('html-react-parser');
22
-
var reactElement = (
23
-
Parser('<p>Hello, world!</p>') // same as `React.createElement('p', {}, 'Hello, world!')`
Parser('<section id="foo" class="bar baz" data-qux="42">look at me now</section>'),
65
99
document.getElementById('root')
66
100
);
@@ -70,122 +104,80 @@ ReactDOM.render(
70
104
71
105
#### replace(domNode)
72
106
73
-
The `replace` method allows you to swap an element with your own React element.
74
-
75
-
The first argument is `domNode`, which is an object which shares the same schema as the output from [htmlparser2.parseDOM](https://github.com/fb55/domhandler#example).
107
+
The `replace` method allows you to swap an element with your own React Element.
76
108
77
-
```js
78
-
Parser('<p id="replace">text</p>', {
79
-
replace:function(domNode) {
80
-
console.log(domNode);
81
-
// { type: 'tag',
82
-
// name: 'p',
83
-
// attribs: { id: 'replace' },
84
-
// children: [],
85
-
// next: null,
86
-
// prev: null,
87
-
// parent: null }
88
-
89
-
return;
90
-
// element is not replaced because
91
-
// a valid React element is not returned
92
-
}
93
-
});
94
-
```
109
+
The first argument is `domNode`, which is an object that has the same output as [htmlparser2.parseDOM](https://github.com/fb55/domhandler#example).
95
110
96
-
Simple example:
111
+
The element is only replaced if a valid React Element is returned.
97
112
98
113
```js
99
-
var Parser =require('html-react-parser');
100
-
var React =require('react');
101
-
102
-
var html = (
103
-
'<div>'+
104
-
'<p id="replace">'
105
-
'replace me'+
106
-
'</p>'+
107
-
'</div>'
108
-
);
109
-
110
-
var reactElement =Parser(html, {
111
-
replace:function(domNode) {
114
+
// with JSX
115
+
Parser('<p id="replace">text</p>', {
116
+
replace: (domNode) => {
112
117
if (domNode.attribs&&domNode.attribs.id==='replace') {
0 commit comments