-
-
Notifications
You must be signed in to change notification settings - Fork 134
TS Error : instanceof Element always return false with Next.js #221
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
Comments
Thanks for opening the issue @purp1eeeee In your CodeSandbox example, you're not returning a React element so replace won't work. See readme. So can you replace the logs with a return of your custom element? export default function IndexPage() {
const html = "<h1>hoge</h1>";
const options: HTMLReactParserOptions = {
replace: (domNode) => {
- console.log(domNode);
- console.log(domNode instanceof Element);
+ if (domNode.name === "h1") {
+ return <h1>replaced</h1>;
+ }
}
};
return <div>{parse(html, options)}</div>;
} See updated CodeSandbox. |
I following this instruction, and I also have same problem, I can't get property Because typescript check And if you check with this condition I have solution for this, maybe can help. I defined the import parse, { HTMLReactParserOptions } from "html-react-parser";
import { Element } from "domhandler/lib/node";
export function contentHandler(postContent: string) {
const options: HTMLReactParserOptions = {
replace: (domNode: Element) => {
if (domNode.attribs) {
if (domNode.attribs.id === 'shortcode') {
return <div className="leadform">Shortcode</div>;
}
}
},
};
return parse(postContent, options);
} |
I can confirm I'm having the same issue with a Next.js project; Typescript type-casting |
@kakaeriel |
Having these issues with a Typescript project. Like @kakaeriel I'm following this instruction about replace including the section on Typescript. The solution from @kakaeriel works for me. The |
Thanks for providing an alternative solution @kakaeriel. Would you be interested in creating a PR to improve the |
@remarkablemark Yes sure! I will create a PR, ASAP. |
Wow. I have the same issue using NextJS.
So why does this only occur in a NextJS app. |
I'm not sure @Naxos84. I bet Next.js has a different TypeScript configuration. Please check out @kakaeriel's solution for a workaround. @kakaeriel let me know if you're able to open a PR to update the |
I don't think that it is NextJs related. So I don't understand why it is suddenly working!? Though it is not working in "my" nextjs project. |
I suggest using a custom typeguard.
|
Hey everyone, I was able to fix this in Next.js 10.x with this solution. It's a public Gist where you can find my current implementation details. The solution has zero type errors in my setup. |
@natterstefan Thanks for sharing your solution! |
I have the same problem with NextJS. Element is always undefined. I typed domNode as any and converted the options type as HTMLReactParserOptions. Element from domhandler doesn't work with SSR? const options = {
replace: (domNode: any) => {
// ...
}
} as HTMLReactParserOptions; |
I'm facing the same issue with replace the function by using your library in Next.js project. The version of HTML-React-parser that I'm using is 1.4.0 import parse, {
attributesToProps,
domToReact,
HTMLReactParserOptions,
Element,
} from 'html-react-parser';
export const ReplaceParser = ({ payload }: { payload: string }) => {
const options: HTMLReactParserOptions = {
replace: (domNode) => {
console.log(domNode instanceof Element); // always returns false
if (domNode instanceof Element && domNode.name === 'a') {
const props = attributesToProps(domNode.attribs);
return (
<a {...props} rel="noopener noreferrer" target="_blank">
{domToReact(domNode.children)}
</a>
);
}
},
};
return <>{parse(payload, options)}</>;
}; |
It's possible to use duck typing to get domNode as an Element: parse(html, {
replace: (domNode) => {
if ('name' in domNode && 'attribs' in domNode) {
// domNode is an Element
}
},
}); |
Reproducible Demo
https://codesandbox.io/s/quizzical-lake-16q4o?file=/pages/index.tsx
When replacing to a custom component, false will always be returned.
I am currently using the any type.
However, is there any other solution other than the any type?
The text was updated successfully, but these errors were encountered: