-
-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
domNode instanceof Element is suddenly false? #633
Comments
It should still work as expected. See CodeSandbox. Try reinstalling your packages: rm -rf node_modules
npm i |
@Jahrhause @remarkablemark I am also having this issue, I have tried reinstalling my packages and it didn't seem to help. For now I have just replaced my |
Thanks for the heads up @jlegreid and sharing your workaround. I'll keep an eye out if more users are experiencing this. |
I'm seeing the same thing. |
Just tried to downgrade to version 2.0.0 and now everything is back to the way it should be, so must be something in version 3. For v.3 I used this workaround as of now:
This way I still get the typings I need. |
Thanks for the updates everyone. Could I ask someone to try regenerating their lockfile as well as nuking node_modules? rm -rf node_modules package-lock.json
npm i Let me know if the error still persists if you try that. |
A bit better, imo, is to use a type predicate: Define a function like function domNodeIsElement(domNode: DOMNode): domNode is Element {
return domNode.type === "tag";
} Then, as for usage, as soon as you've done if (!domNodeIsElement(domNode)) return; any code after that knows that domNode is an Element. |
@remarkablemark I would but I've just set off travelling for a week. Hopefully someone else can test that in the mean time. |
Sounds good @tremby |
Not sure if this is related, but I am getting when using code from readme about typescript - https://github.com/remarkablemark/html-react-parser#replace-with-typescript
|
@TheRarita I think your CodeSandbox link is incorrect? |
@remarkablemark Sorry, and double sorry. I corrected the link and also realized where my mistake was. I did not realize that |
No worries @TheRarita. Thanks for the update. |
Hey @remarkablemark, after upgrading to v2 to v3, I had the same error. Removing and clearing the Any idea why this would seemingly break when |
Okay, an update on the situation. After removing and reinstalling |
@charkour Just confirming, |
Hey, thanks for the quick response. I was initially a little confused and reported the wrong thing. In v3, the script tags will execute when the page is rendered on the server, but not when rendered on the client. In v2, script tags are executed both on the server and client. In both cases the If I have time at work this week, I could make a minimal repro demo if that is helpful. |
@charkour A reproducible demo would definitely be helpful! |
@remarkablemark Hi, I encountered the same issue so I created a simple reproduction. It was originally created with |
…placer - it is a (hopefully temporary) workaround for an issue where the recommended instanceof check doesn't work (see remarkablemark/html-react-parser#633)
@JiriLojda Thanks for creating the reproducible example. I can confirm that your CodeSandbox isn't working correctly, but when I forked and created my own CodeSandbox, it seems to be working as expected. |
…placer - it is a (hopefully temporary) workaround for an issue where the recommended instanceof check doesn't work (see remarkablemark/html-react-parser#633)
faced with same problem. inside the replacer, I am met by a domNode instance of the wrong Element that can be imported from import parse, { Element } from 'html-react-parser'; i created a check inside the replace function, console log({
assert1: domNode.constructor === Element,
assert2: domNode instanceof Element,
Element1: Element,
Element2: domNode.constructor
}); when inspecting in devTools, I found that the links lead to two identical files with the same content:
This seems to be why typescript shows an error and the script doesn't work on the client. I'm surprised the playgrounds in your readme.md doesn't have this problem. so the problem is more likely on my side, maybe due to the lack of some kind of tree-shaking. I haven't tested this in a production build, only in dev mode. Thanks! |
Thanks for sharing your findings @jt3k. In your case, I think performing a type assertion makes sense since |
running into same issue. using latest version of html-react-parser 3.0.15 and using react 18.2. wanted to ask if there are other use cases . it works in other versions like v2 or v1 |
This is the workaround we are using, @haleetwilio // the workaround: https://github.com/remarkablemark/html-react-parser/issues/616
// the bug: https://github.com/remarkablemark/html-react-parser/issues/633
const isElement = (domNode: DOMNode): domNode is Element => {
const isTag = ['tag', 'script'].includes(domNode.type);
const hasAttributes = (domNode as Element).attribs !== undefined;
return isTag && hasAttributes;
}; |
@charkour thank you for quick response. This definitely helps! :) @remarkablemark is there a plan to fix the bug in coming future? or is the use case too small (or the root cause is not clear yet) that we just have to use the workaround method? totally understand if the answer is latter - just want to inform our team accordingly so we can plan ahead aside from this, thank you for the work on this package. it's awesome :) helping us a ton |
@remarkablemark had previously asked for a reproduction and I couldn't consistently reproduce it, but it seemed to happen when npm was caching TS types while switching between versions of this package. |
@haleedev as mentioned above, this bug can't be consistently reproducible, so there is no plan to fix this. The current workaround suffices and it's documented in the README.md |
…placer - it is a (hopefully temporary) workaround for an issue where the recommended instanceof check doesn't work (see remarkablemark/html-react-parser#633)
Running into same issue in v4.0.0, no switching between versions were previously done. |
@sauron918 can you try deleting and reinstalling |
Hi, just used this library for the first time and the same problem appeared randomly. |
@bricejar what version are you using? Can you use the workaround provided by one of the previous commenters? |
@remarkablemark I am using 4.2.5 and yes I am using a workaround for now. Thank you ! |
Maybe there is a way to solve the problem without resorting to a workaround? let's fix it? because it’s clearly obvious what the reason is, instances are created by different Element classes, so inside the replacer function typescript does not recognize it as a correct Element instance |
Installed the package today, got the same problem right away |
Expected Behavior
domNode instanceof Element should be true
Actual Behavior
domNode instanceof Element is false
Steps to Reproduce
After upgrading from version 1.4 to 3.0 domNode instanceof Element is suddenly returning false
Reproducible Demo
What could have happend?
Environment
The text was updated successfully, but these errors were encountered: