[英]HTML-React-Parser returning object - not string?
I'm trying to parse some basic HTML - for example:我正在尝试解析一些基本的 HTML - 例如:
<p style="color: black">Hi Guys!</p>
into JSX code, and I'm using HTML-React-Parser.进入 JSX 代码,我正在使用 HTML-React-Parser。 I've linked it into my HTML like so:
我已经将它链接到我的 HTML 中,如下所示:
<script src="https://unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js">
And I'm attempting to use it here:我正在尝试在这里使用它:
<script type="module">
import bbCodeParser from "./node_modules/js-bbcode-parser/src/simple.js";
const finishUpload = document.querySelector("#uploadFinish");
const getTextButton = document.querySelector(".textUpload");
function getURL() {
var imageURL = document.querySelector(".headerInfo > div:nth-child(5)")
.innerHTML;
var imageURLParse = bbCodeParser.parse(imageURL);
var myRegex = /<img[^>]+src="(https:\/\/[^">]+)"/g;
var imageSRC = myRegex.exec(imageURLParse);
console.log(imageSRC[1]);
}
getTextButton.addEventListener("click", function () {
getText();
});
finishUpload.addEventListener("click", function () {
getURL();
});
function getText() {
var myContent = tinymce.activeEditor.getContent();
var reactCode = HTMLReactParser('<p style="color: black">Hi Guys!</p>');
console.log(reactCode);
}
</script>
<script>
const dateElement = document.querySelector("#date");
var d = new Date();
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
month = months[d.getMonth()];
day = d.getDate();
year = d.getFullYear();
time = new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
dateElement.innerHTML = `Posted on ${month} ${day}, ${year} at ${time}`;
</script>
It works fine, but the problem is that when I run it, it returns me an object like this:它工作正常,但问题是当我运行它时,它会返回一个 object,如下所示:
{…}
"$$typeof": Symbol(react.element)
_owner: null
_self: null
_source: null
_store: Object { … }
key: null
props: Object { style: {…}, children: "Hi Guys!" }
ref: null
type: "p"
<prototype>: Object { … }
And I'm not really sure what to do with this.而且我不确定该怎么做。 All I really need is a simple string.
我真正需要的只是一个简单的字符串。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thanks in advance.
提前致谢。
The HTMLReactParser
converts an HTML string to one or more React elements. HTMLReactParser
将 HTML 字符串转换为一个或多个 React 元素。 So the result will not be a string as you expected:所以结果不会是你预期的字符串:
HTMLReactParser('<p style="color: black">Hi Guys!</p>');
// will be equal to React.createElement('p', { style: 'color: black' }, 'Hi Guys!')
Can check some examples in their document:可以在他们的文档中查看一些示例:
<ul>
{HTMLReactParser(`
<li>Item 1</li>
<li>Item 2</li>
`)}
</ul>
Ref: https://www.npmjs.com/package/html-react-parser参考: https://www.npmjs.com/package/html-react-parser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.