Comments (6)
For the 1st error, can you return void since the return type should either be a React element or void (returning the domNode shouldn't really do anything):
- return domNode // error 1
+ return
For the 2nd error, can you use type assertion:
- return <>{domToReact(domNode.children, options)}</> // error 2
+ return <>{domToReact((domNode as Element).children as DOMNode[], options)}</>
Preferably you should check
domNode instanceof Element
instead ofdomNode as Element
to prevent runtime errors.
You can import Element
and DOMNode
from:
import { Element, DOMNode } from 'html-react-parser';
from html-react-parser.
Thanks, the method works.
from html-react-parser.
I decided to add back the object
return type for replace
option in order to fix error 1. See #1129
from html-react-parser.
Release:
from html-react-parser.
` const options: HTMLReactParserOptions = {
replace: (domNode: DOMNode) => {
if (!(domNode instanceof Element)) return;
if (domNode.name === "ul") {
return (
{domToReact(domNode.children as DOMNode[])}
);
}
if (domNode.name === "li") {
return (
<li className="flex gap-2 items-center ">
<Icons.PinkArrowMarker />
{domToReact(domNode.children as DOMNode[])}
</li>
);
}
},
};`
Only first condition is getting replaced, can anyone help me with this?
from html-react-parser.
@Bkumar48 can you open a new issue and provide a reproducible example?
from html-react-parser.
Related Issues (20)
- How to resize/style the HTML content? HOT 11
- important style is not parsed HOT 13
- Resolution error when using TypeScript with `moduleResolution` set to `node16` or `nodenext` HOT 5
- How to keep a specific element from being transformed by React? HOT 2
- Type of `parse` is `any` HOT 2
- Version 5.1.5 - multiple versions of @types/react causes typescript build errors HOT 6
- Incompatible default exports between ESM and CJS HOT 5
- domNode to HTML string? HOT 2
- Usage in web worker HOT 9
- String text with less than greater than sign are treated as tags. (eg. 4<y<8) HOT 1
- Components outside html component get resized HOT 13
- Next JS Support HOT 7
- Question: Replace node type text by a React Component HOT 2
- Sibling self closing tags become nested HOT 4
- React 19 RC Error HOT 3
- Nested list not rendering HOT 4
- Cannot pass `domNode.children` to `domToReact`, as the docs suggest you can (with Typescript) HOT 3
- Parser is breaking when we are provinding some content <content> HOT 1
- Allow React 19 as a peer dependency HOT 3
- Match react and @types/react versions in peer dependency list HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from html-react-parser.