{"id":2708,"date":"2022-06-18T10:46:17","date_gmt":"2022-06-18T08:46:17","guid":{"rendered":"https:\/\/ekiwi.de\/?p=2708"},"modified":"2022-06-18T10:46:17","modified_gmt":"2022-06-18T08:46:17","slug":"react-html-aus-string-ausgeben","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/index.php\/2708\/react-html-aus-string-ausgeben\/","title":{"rendered":"React: HTML aus String ausgeben"},"content":{"rendered":"<p>HTML aus einer Variable ausgeben mit React.<\/p>\n<p><!--more--><\/p>\n<p>In einer React-Anwendung wollte ich einen String ausgeben, mit HTML-Code. Allerdings gibt React den String standardm\u00e4\u00dfig als reinen Text aus und interpretiert den HTML-Code nicht. Dies d\u00fcrfte auch aus Sicherheitsgr\u00fcnden so sein, damit nicht einfach irgendwas ausgef\u00fchrt wird, was ein Anwender z.B. in ein Eingabefeld hackt.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/06\/html_react_1.jpg\" alt=\"\" width=\"700\" height=\"305\" class=\"alignnone size-full wp-image-2709\" srcset=\"\/wp-content\/uploads\/2022\/06\/html_react_1.jpg 700w, \/wp-content\/uploads\/2022\/06\/html_react_1-300x131.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>HTML-Code in React ausgeben<\/h2>\n<p>Aber es gibt es L\u00f6sung. Hierzu installieren wir das Modul &#8222;html-react-parser&#8220;.<\/p>\n<pre>\r\nnpm install html-react-parser\r\n<\/pre>\n<p>Anschlie\u00dfend importieren wir dieses in unseren Quelltext. <\/p>\n<pre>\r\nimport HTMLReactParser from 'html-react-parser';\r\n<\/pre>\n<p>Nun k\u00f6nnen wir den <a href=\"https:\/\/ekiwi.de\/index.php\/4666\/c-das-erste-vorkommen-eines-strings-in-einen-anderen-string-finden-und-ersetzen\/\" title=\"C#: Das erste Vorkommen eines Strings in einen anderen String finden und ersetzen\">String<\/a> durch den Parser schicken und erhalten als Ergebnis die Ausgabe im HTML-Format.<\/p>\n<pre>\r\n&lt;p&gt;{HTMLReactParser(settings.introText)}&lt;\/p&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/06\/html_react_2.png\" alt=\"\" width=\"800\" height=\"298\" class=\"alignnone size-full wp-image-2710\" srcset=\"\/wp-content\/uploads\/2022\/06\/html_react_2.png 800w, \/wp-content\/uploads\/2022\/06\/html_react_2-300x112.png 300w, \/wp-content\/uploads\/2022\/06\/html_react_2-768x286.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>HTML aus einer Variable ausgeben mit React.<\/p>\n","protected":false},"author":1,"featured_media":2260,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[15],"tags":[50,561],"class_list":["post-2708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-java-script","tag-react"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/2708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/comments?post=2708"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/2708\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media\/2260"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media?parent=2708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/categories?post=2708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/tags?post=2708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}