Comments (5)
Ok, understand the problem with the major breaking change.
So I picked your HTMLReactParser
function and implemented it on my side with the named imports from the html-dom-parser
and html-react-parser/lib/dom-to-react
.
import htmlToDOM from 'html-dom-parser';
import domToReact from 'html-react-parser/lib/dom-to-react';
export const htmlReactParser = (html: string): ReturnType<typeof domToReact> => {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
if (!html) {
return [];
}
return domToReact(htmlToDOM(html));
};
It is a little bit simplified but this seems to be working. It is still a workaround so I hope you will be able to address this problem in some future major release.
Thank you for your suggestions, they help a lot :-)
from html-react-parser.
@literat If I added a named export, would that solve your issue? For example:
import { htmlToReact } from 'html-react-parser';
Which is the same as:
const { htmlToReact } = require('html-react-parser');
from html-react-parser.
I am afraid that this will not work when the default
export will be present. I have tried to import attributesToProps
which are present in a package like import { attributesToProps } from 'html-react-parser';
. This was transformed into var htmlReactParser = require('html-react-parser');
and htmlReactParser.attributesToProps();
in CJS. Again, when I used the package build with this I got the error TypeError: htmlReactParser__namespace.default is not a function
.
Neither will using import { default as parser } from 'html-react-parser';
. Again it will end up as var parser = require('html-react-parser');
.
I have tried to simulate what for instance does Babel with this code and it looks like it treats it the same way.
So every export default Something
will be transformed into exports.default = Something
.
However, the named export should be working when the default
will be removed. I have tried to tweak the package in the node_modules
and for instance exports.htmlReactParser = HTMLReactParser;
is working just fine. Same as for export const htmlReactParser = HTMLReactParser;
So then when I use import { htmlReactParser } from 'html-react-parser';
as you suggested, it will work.
from html-react-parser.
@literat Unfortunately I don't think I'm able to remove the default
export for the time being as that would constitute a major breaking change for everybody.
Since this library exports both ESM and CJS, I wonder if there's an import strategy that works for your use case. Perhaps all you need is to import html-dom-parser
and dom-to-react separately.
from html-react-parser.
@literat Nice solution! Glad that the workaround is working.
from html-react-parser.
Related Issues (20)
- attributesToProps converts attribute with key name "open" always to value `true` HOT 2
- Warning with nextjs use - Extra attributes from the server HOT 3
- If the htmlString is dynamic and contains custom components (with hooks used), an error will be hanpend HOT 1
- missing type for domNode.children HOT 4
- Issue after 5.0.9 update HOT 3
- Cannot Handle Microsoft Meeting Link HOT 3
- Add Current Node Index in `replace` Callback HOT 6
- Replacing domNode with editor element multiple times breaks editor bubble menu HOT 4
- Method transform has wrong current index HOT 5
- Validate props beforing passing the props object to the element HOT 3
- SVG html with large Data URL returns empty SVG code HOT 2
- Importing the lib with a very large size HOT 2
- 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 3
- 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
- domNode to HTML string? HOT 2
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.